RX14 changed the topic of #crystal-lang to: The Crystal programming language | http://crystal-lang.org | Crystal 0.25.1 | Fund Crystal's development: http://is.gd/X7PRtI | GH: https://github.com/crystal-lang/crystal | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/ | Gitter: https://gitter.im/crystal-lang/crystal
<FromGitter> <drosehn> Speaking of overflows, I was working on a program doing some simple math, and wrote a constant which was too big for Int32. The thing was I didn't realize it was too big, because it was some value like `3*21` and it just didn't occur to me that might be greater than `2*31`.
oz has quit [Ping timeout: 256 seconds]
oz has joined #crystal-lang
<FromGitter> <drosehn> So it quietly overflowed, and set the variable to some totally wrong Int32 value. I only caught it because I was writing the same program in both ruby and crystal, and the crystal program was generating the wrong answer.
<FromGitter> <drosehn> Once I *realized* the problem, it was easy to solve with `BigInt.new(3)**21`, but would it be easy to make that a compile-time error?
<FromGitter> <drosehn> Or for a more ambitious wish: Is there some way to tell the compiler that it should default to BigInt instead of Int32 for everything? (although it's easy for me to imagine that kind of global switch might run into significant problems...)
<FromGitter> <aisrael> 👍 for catching overflow at least
return0e has quit [Quit: Leaving...]
Raimondii has joined #crystal-lang
Raimondi has quit [Ping timeout: 264 seconds]
Raimondii is now known as Raimondi
return0e has joined #crystal-lang
<FromGitter> <DanilaFe> I wonder, don't most compilers do constant folding? Does Crystal's compiler? If so, catching the overflow at compile time can't be too bad. (This is all coming from someone with 0 knowledge of the Crystal compiler)
<FromGitter> <bew> A constant in Crystal is not necessarily a constant value, it's just a variable that is assigned at the beginning of the program, and that can't be re-assigned
<FromGitter> <DanilaFe> I meant constant more in the sense of a number literal. A lot of compilers, as far as I know, evaluate expressions such as 4*5 at compile time, because their value is known at that point.
<FromGitter> <asterite> We let llvm do that. We could do it to detect overflow. But then, anyone can override * for numbers...
<FromGitter> <bew> @asterite cr compiler does this for constants though, right?
<FromGitter> <DanilaFe> Ah, of course. Isn't it as simple as ⏎ ⏎ ```struct Int32 ⏎ def *(other) ⏎ # . . . ⏎ end ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=5b627f0974470f5c983afa27]
<FromGitter> <noahlh> Hello everyone! A possibly dumb question that I've come up short on in my research: Is it possible to overload/re-declare/change an existing method on a single instantiated object? I don't want to change the whole class.
<FromGitter> <noahlh> In my case, I'm taking a copy of a `Logger` object (from Amber -- `Amber.settings.logger`) and trying to change the `.info` method to log things in a slightly different way for that `dup`'d instance.
<FromGitter> <DanilaFe> When documenting Crystal code, how would I document both a getter and setter that are generated by `property`?
<FromGitter> <bew> Don't use property, just make a getter and a setter and document them
<FromGitter> <DanilaFe> :( is property poor style?
<FromGitter> <DanilaFe> or is this just a limitation of the fact that property is a macro?
<FromGitter> <bew> Not poor style, just not usable if you absolutely need to document the 2 differently
<FromGitter> <DanilaFe> Got it. Thanks
ua has quit [Ping timeout: 260 seconds]
ua has joined #crystal-lang
Liothen has quit [Ping timeout: 256 seconds]
Liothen has joined #crystal-lang
RX14 has quit [Read error: Connection reset by peer]
moei has quit [Ping timeout: 256 seconds]
ashirase has quit [Ping timeout: 265 seconds]
ashirase has joined #crystal-lang
hightower4 has joined #crystal-lang
RX14 has joined #crystal-lang
<FromGitter> <drinkmorewaters> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5b62e252e8698a2dcaf93389]
<FromGitter> <drinkmorewaters> anything similar in Crystal that i can find?
<FromGitter> <icyleaf> try halite https://github.com/icyleaf/halite
<FromGitter> <drinkmorewaters> @icyleaf nothing builtin to stdlib?
<FromGitter> <drinkmorewaters> That will work though :)
mroth has quit [Write error: Connection reset by peer]
jwaldrip has quit [Remote host closed the connection]
Majost has quit [Remote host closed the connection]
ilovezfs_ has quit [Remote host closed the connection]
<FromGitter> <bararchy> HTTP::Client.get :) @drinkmorewaters
<FromGitter> <bararchy> but halite is really nice too
<FromGitter> <bararchy> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5b62e74779bfdf5c998ab0d8]
<FromGitter> <drinkmorewaters> @bararchy thanks for that!
<FromGitter> <fridgerator> @noahlh No I dont think so
<FromGitter> <fridgerator> You could create your own logger class that extends amber logger, and override the info method there
Raimondii has joined #crystal-lang
Raimondi has quit [Ping timeout: 244 seconds]
Raimondii is now known as Raimondi
return0e has quit [Read error: Connection reset by peer]
return0e has joined #crystal-lang
<FromGitter> <j8r> meh `raise "_\t_"` # => _ _
<FromGitter> <j8r> The tab is changed to a space when we raise
<FromGitter> <straight-shoota> Not true: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5b63280b74470f5c983d65f7]
<FromGitter> <straight-shoota> If you have a different result, there is something wrong
<FromGitter> <j8r> yes there is still the tab true
<FromGitter> <j8r> but it isnt interpreted any more ⏎ `'begin; raise "a\ta"; rescue ex; puts ex; end'` # => a a
<FromGitter> <j8r> I can't get why, even with `to_s` the result is the same
<FromGitter> <straight-shoota> `crystal eval 'begin; raise "a\ta"; rescue ex; puts ex; end' 2>&1 | sed 's/\t/\\t/'` still prints `a\ta`
<FromGitter> <j8r> ohhhh ⏎ `begin; raise ""; rescue ex; puts "a\ta" ; end` #=> a a
<FromGitter> <j8r> We can't print correctly a tab in a rescue block 😕
<FromGitter> <j8r> If we try inside and outside, we have a different result (at least for on Linux)
<FromGitter> <j8r> hum wait
<FromGitter> <j8r> nvm that's my term
<FromGitter> <j8r> I think that's because I've somehow before printed a bin, and this trashed my term
<FromGitter> <j8r> need to open a new one
<FromGitter> <j8r> yes that was this
<FromGitter> <j8r> I was trying on 2 terms, and one was Out. Sorry 💀
<FromGitter> <noahlh> @fridgerator thanks! yeah that was one approach i've been trying. I need to figure out how to copy all the instance variables from the existing instance into a new object of the new class (`.dup` creates another object of the same original class). Is there a way to do this that I'm missing?
<z64> @noahlh - no you need to describe this mapping between two types explicitly. `def initialize(other : OtherClass)`, and `@this = other.that` for each var
<z64> or `def self.new(other : OtherClass)`, wherein you call `new(other.foo, other.bar)` of your own constructor
<FromGitter> <hanneskaeufler> Hey everyone, hoping this is the correct place to ask a newb question: Can I somehow make crystal "generalize" the type of an array to the "parent/abstract" class without manually adding `of Parent`? I'm playing with this fiddle here: https://play.crystal-lang.org/#/r/4ntz
<FromGitter> <hanneskaeufler> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5b633e216d45752f987d87a2]
<FromGitter> <straight-shoota> No. This is precisely what `of Parent` is meant for.
<FromGitter> <straight-shoota> Why would you want to omit it?
<FromGitter> <hanneskaeufler> For some library code
<FromGitter> <hanneskaeufler> if I were to provide the `run` method, I wouldnt want the users to have to add `of Parent` everywhere, reads really verbosely
Groogy has joined #crystal-lang
<FromGitter> <straight-shoota> You can simply loosen the type restriction to `run(cats : Array)`
<FromGitter> <straight-shoota> In the future, there will probably be some extension to the type grammar to specify the generic argument accepting all subtypes of `Cat`
<FromGitter> <noahlh> @z64 thanks! That's precisely the direction I was starting to head. Thanks for the info and confirming. Will pursue that!
<z64> no problem:)
<FromGitter> <hanneskaeufler> @straight-shoota thanks, but I don't think loosening the type works if I provide an empty array as the default argument?
<FromGitter> <straight-shoota> Why not?
<FromGitter> <straight-shoota> The default value is only assigned at the method definition
<FromGitter> <straight-shoota> it doesn't need to be repeated at call sites
<FromGitter> <hanneskaeufler> It doesn't compile as well https://play.crystal-lang.org/#/r/4nu8 (I might miss something obviously)
<FromGitter> <asterite> There's nothing you can do about it. Well, you can always set `Array` as a restriction and then do `array.map(&.as(Cat))` but that's a bit slower of course
<FromGitter> <hanneskaeufler> yeah I saw that in an issue somewhere but hadn't gotten it to work in my lib. If that's the only way then I'll have to give that another shot. Thanks
ua has quit [Ping timeout: 260 seconds]
wontruefree has joined #crystal-lang
ua has joined #crystal-lang
wontruefree has quit [Quit: bye]
wontruefree has joined #crystal-lang
wontruefree has quit [Quit: bye]
DTZUZO has joined #crystal-lang
wontruefree has joined #crystal-lang
<FromGitter> <gustavramestad> Hi! Is there a way to get input from the terminal?
<FromGitter> <talbergs> never tried but reading stdandart input shouldn't work ?
<FromGitter> <gustavramestad> How would that be done?
<FromGitter> <gustavramestad> I figured it out
<FromGitter> <talbergs> show me please, @gustavramestad
<FromGitter> <talbergs> I can't figure that myself =/ ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5b6373de8eb2d52fde38a778]
<robacarp> you can just do `gets`
<FromGitter> <gustavramestad> Yeah I just used i = gets
<robacarp> @talbergs wait, are you looking for a single character? Or reading a line?
<FromGitter> <talbergs> no matter
<robacarp> wait, I'm getting the names all confused
<FromGitter> <talbergs> maybe needs to be built to work? Cos example I gave is not working.
<robacarp> why are you doing a spawn?
<FromGitter> <talbergs> because, the loop (main) is kept at `sleep`
<FromGitter> <talbergs> ```p "press a key" ⏎ l = gets ⏎ puts "You pressed: #{l}\n"``` ⏎ ⏎ this also not works [https://gitter.im/crystal-lang/crystal?at=5b6374bb3bca002dcbb3cd3d]
<FromGitter> <talbergs> @gustavramestad will you share ? :)
<robacarp> I don't think you need a spawn. gets is a blocking operation
<robacarp> `gets` doesn't return until you press enter, and it reads the entire line at once
<FromGitter> <talbergs> ok :) never pressed enter thou :D
<robacarp> you can read one character at a time, but it's a much more involved process
<FromGitter> <talbergs> let's do that in few lines?
akaiiro has quit [Remote host closed the connection]
akaiiro has joined #crystal-lang
<robacarp> uh, one sec
<robacarp> let me see how small I can get this
wontruefree has quit [Quit: bye]
Groogy has quit [Ping timeout: 240 seconds]
<robacarp> for most keys anyway. f-keys require a more complicated multibyte read
<robacarp> control keys can be read that way and inspected with the `.ord` of the Char, and Shift keys come in as expected, but other keyboard modifiers don't easily work in the tty
wontruefree has joined #crystal-lang
wontruefree has quit [Client Quit]
<FromGitter> <straight-shoota> But that can easily break for multiple reasons. If you need non-line-based terminal input, you should probably better use a proper library for this.
<FromGitter> <straight-shoota> For example `readline`
<FromGitter> <straight-shoota> Hacking around `STDIN.raw` is not a good idea
<robacarp> unless you want to learn how a tty works
<robacarp> Wait I'm not sure libreadline will allow you to read a character at a time?
<oprypin> robacarp, no, i think readline is quite the opposite
<oprypin> maybe "curses" was meant, i dunno
<oprypin> robacarp, anyway, samples/2048.cr does it
johndescs has quit [Ping timeout: 268 seconds]
johndescs has joined #crystal-lang
hightower4 has quit [Ping timeout: 248 seconds]
<FromGitter> <straight-shoota> depends on what the exact purpose is
<FromGitter> <straight-shoota> the point is: you should probably use some kind of library for handling tty
<FromGitter> <straight-shoota> for a short sample program it might be fine to use `STDIN.raw`
moei has joined #crystal-lang
hightower4 has joined #crystal-lang
<hightower4> Hey, is there a permissions checking library for crystal, like cancan etc.?