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> 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
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.
<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>
<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?
<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> `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