<quazimodo>
i have a question about crystal's type system; can i say "argument x is anything that implements a method that takes an arg of type whatever, and returns some other type
<quazimodo>
basically without referencing a class or an interface, i jst want to tell it any instance/object with a method 'foo' that takes some y and returns some z
<quazimodo>
this is a common approach in typescript, and i know crystal isn't typescript, but i'm having trouble wrapping my head around how crystal does structural types
<quazimodo>
or do you define an abstract class and then let crystal figure out if some other class 'fits' the definition of that abstract class?
Sankalp has quit [Ping timeout: 252 seconds]
Sankalp has joined #crystal-lang
ur5us has joined #crystal-lang
quazimodo has quit [Ping timeout: 246 seconds]
ur5us has quit [Ping timeout: 260 seconds]
ur5us has joined #crystal-lang
fifr- has joined #crystal-lang
fifr_ has quit [Ping timeout: 260 seconds]
ur5us has quit [Ping timeout: 246 seconds]
<jhass[m]>
As for method arguments, just don't put any type restriction at all. You'll get a compile time error when something is passed that doesn't respond to the methods you call inside. This is called duck typing
quazimodo has joined #crystal-lang
<jhass[m]>
As for instance variables, unfortunately doesn't exist. Your two options are to define a union for all types you wanna allow (potentially as an alias) or to define a module with that method (potentially abstract) and include it in all types you wanna pass (potentially by reopening them if they're not yours)
<quazimodo>
jhass[m]: i disconnected
<quazimodo>
did you say anything befor ethat?
<quazimodo>
ah nvm i found the gitter
quazimodo has quit [Ping timeout: 265 seconds]
Sankalp has quit [Ping timeout: 260 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 272 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 268 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 256 seconds]
Sankalp has joined #crystal-lang
Sankalp- has joined #crystal-lang
Sankalp has quit [Ping timeout: 272 seconds]
Sankalp- is now known as Sankalp
<FromGitter>
<moe:busyloop.net> once you learn the crystal way you'll realize how awfully broken the typescript approach is
<FromGitter>
<moe:busyloop.net> (in their defense, shoehorning types onto an untyped language - and the language being javascript of all things - gotta cut them some slack for even trying...)
Sankalp has quit [Ping timeout: 260 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 268 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 260 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 260 seconds]
hightower4 has joined #crystal-lang
hightower3 has quit [Ping timeout: 260 seconds]
hightower4 has quit [Remote host closed the connection]
hightower4 has joined #crystal-lang
Sankalp has joined #crystal-lang
hightower4 has quit [Ping timeout: 246 seconds]
Sankalp has quit [Ping timeout: 265 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 260 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 264 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 260 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 268 seconds]
_dave has joined #crystal-lang
<_dave>
is there a way to have module-level methods defined in one file applied to files required further down the chain? or should i just have those methods put in another submodule and then require it in the files i'll use those in?
Sankalp has joined #crystal-lang
walez has joined #crystal-lang
Sankalp has quit [Ping timeout: 260 seconds]
Sankalp has joined #crystal-lang
walez has quit [Quit: Leaving]
walez has joined #crystal-lang
Sankalp has quit [Ping timeout: 272 seconds]
<FromGitter>
<Blacksmoke16> 🤔
<FromGitter>
<Blacksmoke16> in order to use methods in a module you have to include the module somewhere, or `extend self` which will make them act as class methods using the module's name as the type
<FromGitter>
<Blacksmoke16> just defining a module with methods in it and requiring that file in other files isnt enough
<FromGitter>
<Blacksmoke16> id like to know more of what you're wanting to do before i give more suggestions
ur5us has joined #crystal-lang
<_dave>
@Blacksmoke16: basically i have a module for my program, let's call it `Game`. in it, i have a die roller method `roll`. i was having problems trying to use that method in other classes *within the same module*, errored as "missing".
<_dave>
i can tell that means that it's not namespacing the way i'm looking.
walez has quit [Ping timeout: 260 seconds]
quazimodo has joined #crystal-lang
<FromGitter>
<moe:busyloop.net> best put some sample code on https://carc.in/#/cr
<FromGitter>
<Blacksmoke16> modules are class scope, so you prob need to do like `def self.roll` then could call it anywhere as `Game.roll` if that helps?
<FromGitter>
<Blacksmoke16> but yea, an example would be 💯
<FromGitter>
<Blacksmoke16> to clarify w/o `self.` prefix, you'd have to import the module its defined in somewhere in order to actually use it
Sankalp has joined #crystal-lang
<FromGitter>
<may> Is there a way to determine if there is data in STDIN at runtime? I'm writing a script that may or may not have data at runtime, but I don't want it to hang on a `gets` call while waiting for input since there isn't going to be any. ⏎ ⏎ I've tried `STDIN.nil? STDIN.peek` and similar but haven't been able to figure it out.
<FromGitter>
<moe:busyloop.net> it's a bad idea. you could in theory use select() to check if there's data - but that fails if the writer hasn't already written
<FromGitter>
<Blacksmoke16> i cant really think of a good way to do it, other than hacking in something with `select` to allow `STDIN` to block in a diff fiber:
<FromGitter>
<moe:busyloop.net> you can also use #isatty to see if stdin is connected to a tty, but that has problems of its own
<FromGitter>
<Blacksmoke16> or maybe this is something you could do with `STDIN.raw`
<FromGitter>
<moe:busyloop.net> @Blacksmoke16: but when do you exit? :)
<FromGitter>
<moe:busyloop.net> it may work for long-running process, but that would be quite a weird contraption. could maybe make sense if it's a log forwarder or such
<FromGitter>
<Blacksmoke16> *never*
hightower2 has joined #crystal-lang
Sankalp has quit [Ping timeout: 256 seconds]
<FromGitter>
<abduct> hello all. i am running into a weird behavior and i am curious if anyone has any input. https://carc.in/#/r/edw1
<FromGitter>
<abduct> im of the understanding that tan-1(0/ANYTHING) should always be 0, but crystal returns PI or 180 degrees for negative divisions
<FromGitter>
<may> Thank you all for the thoughts, I appreciate it!
<FromGitter>
<Blacksmoke16> @abduct you get the same output in Ruby so prob safe to say its expected output?
<FromGitter>
<Blacksmoke16> also realize you're printing both the `atan2` value and then the rest. so the 2nd values are ultimately what you want yea?