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
alexherbo2 has joined #crystal-lang
DTZUZU_ has joined #crystal-lang
DTZUZU has quit [Ping timeout: 246 seconds]
DTZUZU has joined #crystal-lang
DTZUZU_ has quit [Ping timeout: 246 seconds]
Nekka_ has joined #crystal-lang
andremed- has joined #crystal-lang
early` has joined #crystal-lang
andremedeiros has quit [*.net *.split]
johnny101 has quit [*.net *.split]
early has quit [*.net *.split]
Nekka has quit [*.net *.split]
Nekka_ is now known as Nekka
andremed- is now known as andremedeiros
sagax has quit [Ping timeout: 245 seconds]
deavmi has quit [Read error: Connection reset by peer]
alexherbo2 has quit [Quit: The Lounge - https://thelounge.chat]
deavmi has joined #crystal-lang
DTZUZU_ has joined #crystal-lang
DTZUZU has quit [Ping timeout: 256 seconds]
f1reflyylmao has joined #crystal-lang
f1refly has quit [Ping timeout: 256 seconds]
f1reflyylmao is now known as f1refly
chachasmooth has quit [Ping timeout: 260 seconds]
chachasmooth has joined #crystal-lang
deavmi has quit [Ping timeout: 272 seconds]
chachasmooth has quit [Ping timeout: 256 seconds]
chachasmooth has joined #crystal-lang
DTZUZU has joined #crystal-lang
DTZUZU_ has quit [Ping timeout: 265 seconds]
teardown has quit [Remote host closed the connection]
teardown has joined #crystal-lang
<FromGitter> <ImAHopelessDev_gitlab> back in action!
<FromGitter> <ImAHopelessDev_gitlab> time to fire up dem shards 😍
deavmi has joined #crystal-lang
<FromGitter> <naqvis> welcome back Girng 🚀
postmodern has joined #crystal-lang
_ht has joined #crystal-lang
<postmodern> using macros can you iterate over the fields of a struct?
<postmodern> trying to define structs for different pixel formats, and i want to define arithmatic operators that modify each of the pixel color values of the pixel struct (ex: rgb -= 1 would decrement the red, green, blue fields)
<FromGitter> <naqvis> yes you can via using iterate over `@type.instance_vars`
hendursaga has quit [Remote host closed the connection]
<FromGitter> <naqvis> but I doubt you can use them for your usecase
<FromGitter> <naqvis> ivars won't be made available until class/struct is fully loaded
<FromGitter> <naqvis> you can access the fields inside class/struct method
<FromGitter> <naqvis> but don't think you can access them to generate methods
hendursaga has joined #crystal-lang
hendursa1 has joined #crystal-lang
hendursaga has quit [Ping timeout: 268 seconds]
<postmodern> naqvis, can generic types accept an arbitrary list of fields? maybe i could create a generic pixel module or abstract-struct which all other pixel structs use?
<FromGitter> <naqvis> you better define a top level macro which accepts a splat and in that macro iterate through the args and define methods
<FromGitter> <naqvis> you can refer to `record` or similar serialization macros
<FromGitter> <naqvis> `record` macro should be quite similar to your use-case
<FromGitter> <naqvis> postmodern https://carc.in/#/r/ajts
<FromGitter> <naqvis> sth like this
<FromGitter> <naqvis> i believe there might be some other better ways to handle this, but this so far comes to my mind
<FromGitter> <naqvis> you can wait for others to jump in and provide their input
<postmodern> hmm trying to define reusable modules for each field (HSV and HSL both share hue and saturation fields), but crystal doesn't like using property in a module with a generic type
<FromGitter> <naqvis> you can define properties in module then have that module included
<FromGitter> <naqvis> https://carc.in/#/r/ajtv
<postmodern> ops was initializing the wrong field, thus leaving the other field uninitialized
<FromGitter> <naqvis> 👍
<postmodern> doesn't appear that crystal supports Module#included?
<postmodern> `Module#included` hook method for when the module is `include`ed?
<FromGitter> <naqvis> it does
<FromGitter> <naqvis> https://carc.in/#/r/ajtw
<FromGitter> <naqvis> https://carc.in/#/r/ajtx
<FromGitter> <naqvis> `@type` represents the including type
<postmodern> cool!
<postmodern> is there a short-hand for casting a slice of uint8s to a slice of structs made up of uint8s, or should i define my own function for that?
sz0 has quit [Quit: Connection closed for inactivity]
<postmodern> i assume i'd have to do something like Slice(Pixel::RGB).new(buffer.to_unsafe,buffer.buffer.size / sizeof(Pixel::RGB))
<postmodern> er `Slice(Pixel::RGB).new(buffer.to_unsafe.as(Pixel::RGB *), (buffer.size / sizeof(Pixel::RGB)).to_i32)` seems to work
<FromGitter> <naqvis> I would say you are casting your way to land of segfaults :P
<FromGitter> <naqvis> `unsafe` means really unsafe and do it at your own risk and you loose all type safety offered by compiler
<FromGitter> <naqvis> so please use it with caution
<postmodern> https://github.com/postmodern/pix_fmt.cr here's what i have so far, in case anyone is interested
aquijoule_ has quit [Remote host closed the connection]
aquijoule_ has joined #crystal-lang
<postmodern> using macros in modules, is there a way to say an argument type has to be the same type as the class/struct which includes the module?
<postmodern> i guess `self` works if i'm inside a macro, however i want to predefine a bunch of common operators in a module, and then just include that module into multiple structs
fifr` has joined #crystal-lang
<postmodern> unrelated note, it's weird how argument type of Int can access explicit unsigned literals
<postmodern> hmm odd, putting the methods i want to define within a `macro included` block causes the methods to not modify the struct when called upon them. moving the method out of `macro included` fixes it.
<straight-shoota> can you show an example?
<straight-shoota> the non-included method is visited in the Mixin1 scope which doesn't have any ivars
<straight-shoota> hence the method body is empty
<straight-shoota> you can confirm with {{ debug }}
<straight-shoota> oh, that's actually wrong
<straight-shoota> hm
<postmodern> otherway around? Mixin1 and Foo appare to properly add the numbers, but Bar/Mixin2 is unchanged
<straight-shoota> ah yeah
<straight-shoota> the macro for loop is expanded before the included hook
<straight-shoota> you need to escape it or place it in {% verbose %}
<straight-shoota> so it get's evaluated later
<straight-shoota> https://carc.in/#/r/ajvi
<postmodern> so in the first example with add!(other : self) would `self` match any object that includes the module, or the specific class that included the module which we're calling .add! on?
<postmodern> hmm you can't define a generic class that inherits from another generic class? trying to subclass Slice
<FromGitter> <Blacksmoke16> that sounds like a bad idea
<FromGitter> <Blacksmoke16> just make a new generic class that wraps a slice, delegating methods to the internal one
<FromGitter> <ImAHopelessDev_gitlab> just create your own method!
<yxhuvud> imahopelessdev: Wrapping the struct in a class or struct tends to be a lot cleaner.
alexherbo2 has joined #crystal-lang
yxhuvud has quit [Quit: No Ping reply in 180 seconds.]
yxhuvud has joined #crystal-lang
hightower2 has joined #crystal-lang
<hightower2> Hey, so I am to write a program which talks a certain protocol. Messages are basically binary data of defined structure. As a first thing I should write some serializer and deserializer for them... Are there some generic interfaces provided within which I should do this, or it's an open field?
<hightower2> (by 'provided' I mean within Crystal or the ecosystem)
<FromGitter> <Blacksmoke16> https://github.com/j8r/crystalizer can do that
<FromGitter> <jrei:matrix.org> and if you want to have messages, you can use https://github.com/j8r/crystal-byte-protocol, which will wrap the above library
<hightower2> quite wonderful advice, thanks
<FromGitter> <jrei:matrix.org> it is essentially a wrapper which use the first byte to know which is the message
<FromGitter> <jrei:matrix.org> it will be hard to make something simpler, but there are limitations
alexherbo2 has quit [Read error: Connection reset by peer]
alexherbo2 has joined #crystal-lang
hendursa1 has quit [Ping timeout: 268 seconds]
hendursaga has joined #crystal-lang
<hightower2> Well, two questions -- (1) is carc.in not working as expected when one clicks "Compile & run" for anyone else? For me just gets me back to input page, without showing results or assigning the URL
<hightower2> and (2) having to do it here, why: f = Crystalizer::ByteFormat; f.serialize(obj) works while f = Crystalizer::ByteFormat.new; f.serialize(obj) returns Nil?
<hightower2> from the docs it seems both should be supported, and I need the version with the object so I can set byte format to BigEndian
alexherbo21 has joined #crystal-lang
<hightower2> aha of course, figured out (2)
alexherbo2 has quit [Ping timeout: 246 seconds]
alexherbo21 is now known as alexherbo2
hendursa1 has joined #crystal-lang
hendursaga has quit [Ping timeout: 268 seconds]
hendursa1 has quit [Ping timeout: 268 seconds]
hendursa3 has joined #crystal-lang
yxhuvud has quit [Quit: No Ping reply in 180 seconds.]
yxhuvud has joined #crystal-lang
<FromGitter> <anthonyshull> is there a way to instantiate HTTP::Client with default headers? or should i just wrap it?
<FromGitter> <Blacksmoke16> https://crystal-lang.org/api/master/HTTP/Client.html#before_request(&callback:HTTP::Request-%3E)-instance-method
<FromGitter> <anthonyshull> so nice, thanks!
fifr` has quit [Ping timeout: 260 seconds]
<hightower2> jrei: soo, in short, if I use the Point example from Crystalizer, why with YAML I can do a serialize to io and back to: Point, but if I try with ByteFormat, the part to: Point tells me: rror: no overload matches 'Crystalizer::ByteFormat#deserialize' with types IO::Memory, to: Point.class, Overloads are: ... <all builtin types>, - Crystalizer::ByteFormat#deserialize(to type : T.class)
<hightower2> solved, thanks
alexherbo24 has joined #crystal-lang
alexherbo2 has quit [Read error: Connection reset by peer]
alexherbo24 is now known as alexherbo2
<FromGitter> <jrei:matrix.org> tell me if anything can be improved
<FromGitter> <jrei:matrix.org> the docs could have rough edges
<hightower2> I found the API a bit strange, like byte_format's deserialize only working on builtin @io, but OK
<hightower2> the issue I have now is... I am trying to deserialize the IO which I serialized, and it gives me:
<FromGitter> <jrei:matrix.org> for byteformat yes, a bit special/lower lever
<FromGitter> <jrei:matrix.org> *level
<hightower2> {% raise "Crystalizer::ByteFormat does not support unions; the protocol requires unambiguous field types (#{T.union_types})." %}
<FromGitter> <jrei:matrix.org> that's because usually we use byteformat with IOs
<FromGitter> <jrei:matrix.org> yes, unions are not supported
<hightower2> ^-- in this line I found it useful to include which union types those are... because it didn't specify field name
<FromGitter> <jrei:matrix.org> not sure there is a way to print the field... no rescue possible in macros :/
alexherbo2 has quit [Client Quit]
<FromGitter> <jrei:matrix.org> even when using JSON/YAML, unions are not ideal because the value has to be parsed to a type, and then rescue and try the other type, etc
<hightower2> ok this is fantastic, got the byte header to serialize/deserialize. The only issue I see now is the union, but I'll probably handle this separately. (i.e. if I read in the header that body exists, then additionally deserialize body in a second call to deserialize)
<FromGitter> <jrei:matrix.org> yes there are workaround for unions
<FromGitter> <jrei:matrix.org> you can use another message, or an enum
<FromGitter> <jrei:matrix.org> (the enum will tell how to parse the subsequent bytes)
<FromGitter> <jrei:matrix.org> personally I usually prefer to have a module, and include it in N similar types
<hightower2> if I understand you right, since I am adapting to an existing protocol, the enum trick is not possible, right?
<FromGitter> <jrei:matrix.org> an existing protocol?
<hightower2> yes, I am not just serializing/deserializing because I want it, but because I'll be getting data in this format from a non-crystal source
<hightower2> and can't change anything in it
<FromGitter> <jrei:matrix.org> so, you'd like to try to parse bytes, and then if there is an error, try again with an other (that's how JSON/YAML do)
<FromGitter> <jrei:matrix.org> hum...
<hightower2> no I mean, in the header, which is always the same, I read whether body exists. So I first deserialize just the header, read whether the body exists, and if yes, run another deserialize (to a different class or struct) to parse the body part
<hightower2> That's my current idea how I'd do it.
<hightower2> And I'm asking whether am I correct that your idea with an Enum does not apply to this case, because I can't change anything in this defined protocol.
<FromGitter> <jrei:matrix.org> yes that's how to do it
<FromGitter> <jrei:matrix.org> the enum idea is from https://github.com/j8r/crystal-byte-protocol
<hightower2> right, right
<hightower2> ok great, thanks for the discussion
<FromGitter> <jrei:matrix.org> ..Well the enum was an internal implementation in this case
<FromGitter> <jrei:matrix.org> but I find it nice for API docs :)
<hightower2> jrei: any idea why this code would result in compiler error? https://bpa.st/6GGA
<hightower2> or rather, compiler crash
<hightower2> it happens with 0.35.1 and 0.36.1
<hightower2> it does not happen if the last 2 lines are commented, or if command_status is defined as UInt32 instead of CommandStatus enum
<FromGitter> <jrei:matrix.org> let me see
<FromGitter> <anthonyshull> is there a way to pass functions in to map without having to use an anonymous wrapper?
fifr` has joined #crystal-lang
<FromGitter> <anthonyshull> i guess i can use `&.downcase`
<FromGitter> <jrei:matrix.org> also no need to require json/yaml
<hightower2> jrei: yes, yes, that's aleftover from early testing
<FromGitter> <jrei:matrix.org> I got `Bytes[0, 0, 0, 47, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1]`
<FromGitter> <jrei:matrix.org> (and removing the last line)
_ht has quit [Remote host closed the connection]
<hightower2> that's OK, that's the first part which works fine
<hightower2> but when it reaches the 'deserialize' line, it crashes
<FromGitter> <jrei:matrix.org> ha sorry
<FromGitter> <jrei:matrix.org> copy paste miss :/
<FromGitter> <jrei:matrix.org> no easy to copy all the text in your paste bin (or I miss a button)?
<FromGitter> <jrei:matrix.org> I think that's due to enums
<hightower2> yes, as mentioned it doesn't crash if the second enum (CommandStatus) is defined as UInt32 instead of that enum
<hightower2> but... where is the bug, in Crystal or crystalizer?
<FromGitter> <jrei:matrix.org> oh yes that was the enums, work with u32
<FromGitter> <jrei:matrix.org> I may have done something wrong for enums in my lib
<FromGitter> <jrei:matrix.org> which, by the way, mess a bit with the language (nothing too )
<FromGitter> <jrei:matrix.org> nothing too bad
<FromGitter> <jrei:matrix.org> your code should work, I let you know for the fix
<hightower2> awesome, thank you!
<FromGitter> <anthonyshull> i can't find anything in the docs about how to do a global regex
<FromGitter> <Blacksmoke16> hm?
<FromGitter> <anthonyshull> like, i want to match all title cased words
<FromGitter> <anthonyshull> `TagCreateParams` i want to get Tag Create and Params
<FromGitter> <Blacksmoke16> https://crystal-lang.org/api/master/String.html#scan(pattern:Regex,&)-instance-method
<FromGitter> <anthonyshull> sweet, that worked
<FromGitter> <anthonyshull> is there a better way to alias long class names? or do you have to use alias?
<FromGitter> <Blacksmoke16> alias
<FromGitter> <anthonyshull> 👍 it's been three months since i've touched any crystal
<FromGitter> <anthonyshull> so i'm having to re-learn it
<FromGitter> <Blacksmoke16> but id argue just dealing with the classname is easier, unless its a really common one
<FromGitter> <anthonyshull> just super long
<FromGitter> <anthonyshull> ShipEngine::RPC::Services::TagCreateParams
<FromGitter> <jrei:matrix.org> hightower2: got it
<FromGitter> <jrei:matrix.org> `Enum.class` does not seem to take the `Command` enum type
<FromGitter> <jrei:matrix.org> so it ends up in `deserialize(to type : T.class) : T forall T`
<FromGitter> <jrei:matrix.org> in fact no, the compile think it could be
<FromGitter> <jrei:matrix.org> I push a fix and submit a bug
<FromGitter> <Blacksmoke16> @anthonyshull ah so its the FQN that's long
<FromGitter> <Blacksmoke16> in that case you could do something like what i did for Athena. `alias SRS = ShipEngine::RPC::Services`. then `SRS::TagCreateParams`
<FromGitter> <Blacksmoke16> alias common namespaces versus the actual type
<hightower2> thanks jrei
<FromGitter> <jrei:matrix.org> interestingly, it works with 1 enum but not 2
<FromGitter> <jrei:matrix.org> e.g. if I remove `command_status`, it works
<hightower2> right
<hightower2> Is someone around to fix carc.in? It's responding with HTTP 500 here
postmodern_ has joined #crystal-lang
postmodern has quit [*.net *.split]
<FromGitter> <jrei:matrix.org> I think the issue is https://github.com/crystal-lang/crystal/issues/8973
<FromGitter> <jrei:matrix.org> incorrect overload chosen when forall is used
<FromGitter> <jrei:matrix.org> I've hard time to reduce it, so I'll just push the fix