ChanServ changed the topic of #crystal-lang to: The Crystal programming language | https://crystal-lang.org | Fund Crystal's development: https://crystal-lang.org/sponsors | GH: https://github.com/crystal-lang/crystal | Docs: https://crystal-lang.org/docs | Gitter: https://gitter.im/crystal-lang/crystal
ur5us has joined #crystal-lang
straight-shoota has quit [Ping timeout: 255 seconds]
jhass has quit [Ping timeout: 260 seconds]
straight-shoota has joined #crystal-lang
jhass has joined #crystal-lang
_ht has joined #crystal-lang
_ht has quit [Remote host closed the connection]
ur5us has quit [Ping timeout: 255 seconds]
Sankalp has quit [Ping timeout: 268 seconds]
Sankalp has joined #crystal-lang
ur5us has joined #crystal-lang
<FromGitter> <azurelmao> Hello 👋 I'm learning Crystal and I don't know how to declare a StaticArray.
<FromGitter> <azurelmao> I assumed doing StaticArray(Foo, 30).new would work, but it says "Error: private method 'new' called for StaticArray(T, N).class ⏎ "
walez has joined #crystal-lang
<frojnd> @azurelmao for numbers you can use Int32.static_array(30)
<FromGitter> <azurelmao> Oh, I see. Thank you!
ur5us has quit [Ping timeout: 255 seconds]
<frojnd> How could I improve this looping over digits and at the same time suming results without local sum var? https://carc.in/#/r/dot0
yxhuvud has quit [Read error: Connection reset by peer]
<Sankalp> @frojnd reduce, perhaps? https://crystal-lang.org/api/1.5.0/Enumerable.html#reduce(&)-instance-method
<frojnd> Oh nice! Sankalp
yxhuvud has joined #crystal-lang
<frojnd> Although I need index
<Sankalp> Does come with index `[1, 2, 3, 4, 5].reduce { |acc, i| acc + i }`
ur5us has joined #crystal-lang
ur5us has quit [Ping timeout: 244 seconds]
walez has quit [Ping timeout: 252 seconds]
ur5us has joined #crystal-lang
<FromGitter> <azurelmao> Why won't this work? https://play.crystal-lang.org/#/r/dovi
ur5us has quit [Ping timeout: 244 seconds]
yxhuvud has quit [Read error: Connection reset by peer]
yxhuvud has joined #crystal-lang
<frojnd> Sankalp: `i` in this case is the _value_ consider this: [0,0,1].reduce {|acc, i| (acc*2**i) + i } I would expect this to sum to 4, if index stats with 0, but i is the value
<frojnd> So not sure how to achieve index with reduce
<Sankalp> Oh my bad
<Sankalp> I thought `i` was the index mistakenly, but you are right it's the value
<frojnd> Maybe combination of reduce + each.with_index somehow
<Sankalp> yeah, but you would have to keep the accumulator outside the closure
<frojnd> Not sure how
<frojnd> Something like this maybe: `[0,0,1].each_with_index.reduce(0) { |acc, (d, i)| d*2**i + acc}`
<Sankalp> yeah, that's even better
<FromGitter> <azurelmao> Sankalp: But then the type is (Bar | Nil), which is what I'm trying to avoid because I have to do .as(Bar) every timr I use it, and what's more is I can't circumvent it by including the logic in the getter https://play.crystal-lang.org/#/r/dowa
<Sankalp> @azurelmao, think about it: you are trying to assign to a class variable the instance of the class. The class variable will be created before the instance variable, so what value will it have before the instance variable is assigned to it?
<Sankalp> s/instance variable/instance/
<Sankalp> To put it simply, class variable is created first, instance is created later
<Sankalp> So before the assignment, naturally the class variable must take some value
<Sankalp> If you don't want it to have a Nil value, try giving a default
<FromGitter> <azurelmao> Okay. Then can I somehow set an empty default value to @@bar, that way the type won't be nilable?
<Sankalp> yes
<FromGitter> <azurelmao> like a fake Bar with nothing in it
<Sankalp> exactly
<FromGitter> <azurelmao> Well, I tried and got an Unhandled Exception
<Sankalp> Maybe because it's recursive
<Sankalp> yeah, it throws a recursive exception
<FromGitter> <azurelmao> I guess there's no way to do it
<FromGitter> <azurelmao> I would be fine if returning bar.as(Bar) actually returned Bar
<Sankalp> Idk if this counts https://play.crystal-lang.org/#/r/dowl
<frojnd> Any ideas how can I build crystal for arm32? https://crystal-lang.org/reference/1.5/platform_support.html#tier-2 It should be supported just can't find some more info. Would like to put it on raspberry pi 1
<FromGitter> <mattrberry> How do I compare types in a macro? For example, I have something like ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=630f88a311a6a83d04912ebd]
<FromGitter> <Blacksmoke16> try `v <= Bool`
<FromGitter> <mattrberry> `Error: undefined macro method 'Path#<='`
<FromGitter> <Blacksmoke16> `v.resolve <= Bool`
<FromGitter> <mattrberry> Sweet yeah just found that in the docs
<FromGitter> <mattrberry> Ty!
walez has joined #crystal-lang
_ht has joined #crystal-lang
_ht has quit [Remote host closed the connection]
walez has quit [Ping timeout: 255 seconds]
ur5us has joined #crystal-lang
walez has joined #crystal-lang
walez has quit [Quit: Leaving]
oprypin has quit [Quit: Bye]
oprypin has joined #crystal-lang
<FromGitter> <mattrberry> Is it possible to see the values of an enum in a macro?
<FromGitter> <Blacksmoke16> iterate over its constants
<FromGitter> <mattrberry> I see that I can get `enum_path.resolve.constants`, but how can I get the constant's value from the MacroId?
<FromGitter> <mattrberry> I need to find the value of the highest enum value
<FromGitter> <Blacksmoke16> what macroId?
<FromGitter> <Blacksmoke16> oh
<FromGitter> <Blacksmoke16> pass it to `parse_type`
<FromGitter> <Blacksmoke16> may need to stringify the macroId first
<FromGitter> <mattrberry> What should I be passing to `parse_type`? I already have a path
<FromGitter> <mattrberry> ``` {% for constant in type.resolve.constants %} ⏎ {% p constant %} ⏎ {% end %}``` ⏎ ⏎ Here the constants are `MacroId`s printing them yields the constants' names, but I'm hoping to get the values [https://gitter.im/crystal-lang/crystal?at=630fee0399949962934244e9]
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=630fee186837563d1cb0ff66]
<FromGitter> <Blacksmoke16> can just do this actually
<FromGitter> <mattrberry> Ah sweet, just found that too :) Thanks!
<FromGitter> <Blacksmoke16> 👍
<FromGitter> <mattrberry> Is it possible to exit a macro loop early?
<FromGitter> <Blacksmoke16> no
<FromGitter> <Blacksmoke16> either filter the data first or wrap everything in a conditinoal
ur5us has quit [Ping timeout: 244 seconds]
<FromGitter> <mattrberry> I just want to calculate the number of bits required to represent the values in an enum. I'm currently doing that by finding the max enum value, then counting the number of bits stupidly by multiplying a baseline number of 2 and counting up every time that's still less than the max enum value. It's pretty easy to get that to blow up compile-time if the enum values are large, though. Can you think of a better
<FromGitter> ... approach?
<FromGitter> <mattrberry> This is what I'm doing now, which kinda sucks :p Maybe I'm just taking the wrong approach ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=630ff51d999499629342503c]
<FromGitter> <Blacksmoke16> hmm
<FromGitter> <Blacksmoke16> and this would be diff than `sizeof(MyEnum)`?
<FromGitter> <Blacksmoke16> given an enum is just an int type. reminder you can also do like `enum MyEnum : Int8` if that helps