ChanServ changed the topic of #crystal-lang to: The Crystal programming language | | Fund Crystal's development: | GH: | Docs: | Gitter:
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?
yxhuvud has quit [Read error: Connection reset by peer]
<Sankalp> @frojnd reduce, perhaps?
<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?
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
<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 actually returned Bar
<Sankalp> Idk if this counts
<frojnd> Any ideas how can I build crystal for arm32? 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``` []
<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 []
<FromGitter> <Blacksmoke16> ```code paste, see link``` []
<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``` []
<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