jhass changed the topic of #crystal-lang to: The Crystal programming language | https://crystal-lang.org | Crystal 0.35.1 | 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
zorp has quit [Read error: Connection reset by peer]
deavmi has quit [Ping timeout: 256 seconds]
deavmi has joined #crystal-lang
f1refly has joined #crystal-lang
f1reflyylmao has quit [Ping timeout: 272 seconds]
<FromGitter> <mattrberry> Another macro question from me 😬 I'm currently looking to make a class for bitfields, but one that works a little differently than what I've found in bitfields.cr (https://github.com/elorest/bitfields/blob/master/src/bitfields.cr) and bindata.cr (https://github.com/spider-gazelle/bindata/blob/master/src/bindata.cr). Namely, I'm trying to define it in a way that allows me to do something along the lines of
<FromGitter> ... ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ I'd love to remove that requirement. I'm trying to understand if there's a way to keep track of the lengths that have been exhausted by each field *at compile-time* rather than at runtime. Is there a convenient way to do this? Could anyone help me with this or direct me where to find so ... [https://gitter.im/crystal-lang/crystal?at=5f66cf03a857200e6d704bd1]
<FromGitter> <mattrberry> And hopefully that'll also let me give a compile-time error messages if the inheriting class specifies too few or too many bits in its fields
_whitelogger has joined #crystal-lang
<FromGitter> <mattrberry> Hmm, I guess I can create a constant `CURRENT_POS = [0]` and just update the value inside the array..
<FromGitter> <mattrberry> Is there a way to access the size of the generic type in the macro? E.g. in the example above, get the size of `UInt8`?
johnny101 has joined #crystal-lang
<FromGitter> <mattrberry> Okay I got everything working to my satisfaction except for getting the size of the generic type.. Anybody know how I might do that?
_whitelogger has joined #crystal-lang
<oprypin> for me `crystal doc` doesn't produce 'View source' links anymore. how can i diagnose why this is happening?
johnny101 has quit [Ping timeout: 272 seconds]
<FromGitter> <j8r> interesting, in js we can have achieve something equivalent to yielding blocks/passing proc by using callbacks
<FromGitter> <j8r> ```func(first, second, (reason) => { ⏎ console.log(reason) ⏎ })``` [https://gitter.im/crystal-lang/crystal?at=5f67596cce5bbc7ffdd893d4]
<FromGitter> <j8r> disclaimer: I'm a noob in JS :P
<FromGitter> <j8r> last time I did serious JS was long time ago, 5 years maybe.
<FromGitter> <j8r> JS is a bit better now than I remember...
<FromGitter> <j8r> CSS is still as tricky though :/
zorp has joined #crystal-lang
johnny101 has joined #crystal-lang
zorp has quit [Ping timeout: 260 seconds]
johnny101 has quit [Quit: ZNC 1.7.5 - https://znc.in]
johnny101 has joined #crystal-lang
<FromGitter> <Blacksmoke16> i was able to work around the compiler error from yesterday by not using `Range` type, but just storing begin/end
<FromGitter> <Blacksmoke16> related, i wish there was a type that told the compiler to use the default value
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/9qik in order to not have to provide default values for both constructors
johnny101 has quit [Ping timeout: 272 seconds]
johnny101 has joined #crystal-lang
<FromGitter> <Blacksmoke16> in other news, got this working
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f677072a857200e6d71b672]
johnny101 has quit [Ping timeout: 260 seconds]
johnny101 has joined #crystal-lang
zorp has joined #crystal-lang
<FromGitter> <asterite> Julien: blocks and anonymous functions are different
<FromGitter> <j8r> @asterite The result is similar in my case, no?
<FromGitter> <j8r> If you prefer, similar to passing procs in Crystal. At the end, using inlined code or calling a function pointer, no much difference I see.
Human_G33k has joined #crystal-lang
HumanG33k has quit [Ping timeout: 240 seconds]
<FromGitter> <mattrberry> > *<yxhuvud>* You mean like https://crystal-lang.org/reference/syntax_and_semantics/sizeof.html ? ⏎ ⏎ I'd love to get the size of the generic type at compile time if possible
<FromGitter> <mattrberry> So for example, I'm looking to get something like this ⏎ ⏎ ```class BitField(T) ⏎ macro inherited ⏎ POS = [sizeof(T)] ⏎ end ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=5f678f0189b38d09213db93a]
<FromGitter> <mattrberry> Which errors with the following: `Error: undefined macro method 'SizeOf#-'`
lanodan has quit [Read error: Connection reset by peer]
lanodan has joined #crystal-lang
<FromGitter> <Blacksmoke16> yea you cant get that in macroland afaik
<FromGitter> <Blacksmoke16> but did you try just doing `POS = sizeof(T)`?
<FromGitter> <Blacksmoke16> or try wrapping it in `{% begin %} / {% end %}`?
<FromGitter> <mattrberry> As far as I can tell, neither of those accomplishes what I'm looking for :/
<FromGitter> <mattrberry> I can see that at compile-time I can get `@type.superclass.type_vars[0]` to get the generic type var, I just need the size of that. That information should *theoretically* be available at compile time, right?
zorp has quit [Ping timeout: 272 seconds]
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/9qjw
<FromGitter> <Blacksmoke16> afaik idt you can get it at compile time like that
<FromGitter> <Blacksmoke16> i also think `T` is only available in context of a method
<FromGitter> <mattrberry> Bummer :/ I need to know the size of the generic type in order to know how to pack the bits at compile time
alexherbo2 has joined #crystal-lang
<FromGitter> <Blacksmoke16> :shrug:
<FromGitter> <mattrberry> I guess I can just make another macro on the BitField class that allows the inheriting class to just specify a size, assume that's valid at compile time, then verify it when the inheriting class is instantiated
<FromGitter> <Blacksmoke16> do you need the size for some calculations in a macro at compile time? or you just trying to have a way to expose the size based on `T`?
<FromGitter> <mattrberry> I need the size for calculations at compile time. At least the way I'm currently approaching it is that I use the POS to determine how many bits to shift off, and that calculation is done at compile time since I don't want the logic to exist at runtime
<FromGitter> <Blacksmoke16> gotcha
<FromGitter> <mattrberry> I'd also like to use it at compile-time so that I can alert if the wrong number of bits is specified like this ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f679b46d993b837e0773e30]
<FromGitter> <mattrberry> ^ and not have to hard-code the `8`
<FromGitter> <Blacksmoke16> are you doing like
<FromGitter> <Blacksmoke16> `class SomeClass < BitField(Int32)` or is the child class also generic?
<FromGitter> <mattrberry> `class SomeClass < BitField(Int32)`
<FromGitter> <mattrberry> ```class TestField < BitField(UInt8) ⏎ num :four, 4 ⏎ bool :bool ⏎ num :three, 3 ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=5f679bccf969413294f9efd7]
<FromGitter> <Blacksmoke16> im not sure if there is a reason why `sizeof` can't be used in macro land, or if it just hasn't been implemented
<FromGitter> <mattrberry> Seems like a pretty niche use case, so I wouldn't be surprised if it was the latter
<yxhuvud> the result of sizeof may depend of running other macros in the system.
<yxhuvud> So there is a lot of potential headache that is avoided by not having it.
ua has quit [Ping timeout: 272 seconds]
<FromGitter> <asterite> Julien: try doing return inside an anonymous function and compare that do doing return from a block
<FromGitter> <asterite> sizeof in macros can't work because macros run even before a type hierarchy is defined, or even before instance vars are defined
ua has joined #crystal-lang
<FromGitter> <j8r> @asterite yep, not the same
<FromGitter> <mattrberry> Gotcha, thanks for the info @asterite! I got my macro working without having to change the client code, I just had to push a couple of the compile-time error messages into the runtime
zorp has joined #crystal-lang
sagax has quit [Ping timeout: 272 seconds]
alexherbo2 has quit [Quit: The Lounge - https://thelounge.chat]