<FromGitter>
<Blacksmoke16> If you try to access them before setting it'll segfault like you saw before
<FromGitter>
<evvo> Yes, sorry - I meant that it will be generally safe if all variables are set before accessing them
<FromGitter>
<Blacksmoke16> I suppose
<FromGitter>
<evvo> Works perfectly for my use case, it seems, however that segfault "around the corner" is quite a burden
<FromGitter>
<Blacksmoke16> Indeed
<FromGitter>
<watzon> Is there something like `Enumerable#in_groups_of`, but that doesn't ensure the returned array contains exactly N items?
<FromGitter>
<watzon> Because `in_groups_of` will fill the remaining slots with something, like nil values, if there aren't enough items left
renich has joined #crystal-lang
<ryanprior>
watzon: you can cast an enumerable to an iterator with `.each` and then call `.first(N)` until `.empty?` is true, collecting the results.
renich_ has joined #crystal-lang
renich has quit [Ping timeout: 272 seconds]
renich_ is now known as renich
nowhereFast has joined #crystal-lang
<nowhereFast>
https://play.crystal-lang.org/#/r/97dj on ln:17 a Hash is turned into a String representing Json, then into a JSON::Any. Is there a more direct way to cast Hash to JSON::Any?
<FromGitter>
<Blacksmoke16> whats the reasoning for wanting to store it as `JSON::Any` in the first place?
<FromGitter>
<Blacksmoke16> normally you want a smaller union, not a bigger one
<nowhereFast>
the source could be externally provided JSON, or internally generated data. In the example, include JSON::Serializable is used to define a DTO that unifies both. Is there a more fitting way of modelling this?
<nowhereFast>
when externally provided, we're just carrying it along, when internally provided (and the content of the blob here could vary), we'd like it to go along with the dto.
<nowhereFast>
the dto ends up getting turned back to json and passed on to a different destination
<FromGitter>
<Blacksmoke16> is the structure of the data the same?
<FromGitter>
<Blacksmoke16> just can be generated in two places?
<nowhereFast>
no, the structure is not, which is what led me down the path of trying to leverage JSON::Any for this
<FromGitter>
<Blacksmoke16> could you just generate a JSON string instead of a hash
<FromGitter>
<Blacksmoke16> then `.from_json` would work for both cases
<FromGitter>
<Blacksmoke16> if the structure is dynamic it'll be hard to use types to model the data
<nowhereFast>
That could be done with the hash, but I'm thinking of using named tuples for the internally generated variants for the benefit of having types while it flows through the system
<FromGitter>
<Blacksmoke16> if thats an option it would be better, but those cannot be built at runtime
<FromGitter>
<Blacksmoke16> could possibly use a generic type, like `Container.new internal_data`
<FromGitter>
<Blacksmoke16> where its
<FromGitter>
<Blacksmoke16> ```record Container(T), data : T```
<FromGitter>
<Blacksmoke16> idk, i dont really understand the situation enough
<nowhereFast>
the internally generated blobs can be defined as named tuples, there would be a few different variants, which we care about to the point that they get added to the DTO
<FromGitter>
<Blacksmoke16> do you know what type it'll be before hand?
<FromGitter>
<Blacksmoke16> because you could have multiple ivars on the dto, then assign the internal data to the correct one based on the variant it is
<FromGitter>
<Blacksmoke16> is the structure the same for each variant? Or is it totally different every time?
<nowhereFast>
well, you've given me record and ivar to look into
<nowhereFast>
the variants could be totally different
<FromGitter>
<Blacksmoke16> alright, so deff not modelable via `JSON::Serializable`
<nowhereFast>
I don't need them to be by the time they get to the DTO
<FromGitter>
<Blacksmoke16> i dont suppose you could share what the data looks like?
<nowhereFast>
it's Keys and values, no nesting, with no consistency across keys or value types
<FromGitter>
<Blacksmoke16> i mean like cant even say the keys/values are all either a string, bool, or number?
<nowhereFast>
per case yes, but not across cases
<FromGitter>
<Blacksmoke16> if there's not nesting, what other options would there be?
<FromGitter>
<Blacksmoke16> like arrays or?
<nowhereFast>
values could be number, string, bool, arrays of the same might be a possibility
<FromGitter>
<Blacksmoke16> because you could create a hash of that type
<FromGitter>
<evvo> @Blacksmoke16 , so, from what I read in the GitHub issue, currently there is no way for this to be done ?
<FromGitter>
<Blacksmoke16> correct
<FromGitter>
<evvo> In that case, how to prevent writing a lot of code in certain macros ? Some macros might reach 100+ lines easily if code is not extracted ?
<FromGitter>
<Blacksmoke16> you cant
<FromGitter>
<Blacksmoke16> just have to deal with it
<FromGitter>
<Blacksmoke16> you can use variables and stuff, but currently cant extract common logic, so you would have to duplicate it
<FromGitter>
<Blacksmoke16> not ideal but :shrug:
<FromGitter>
<Blacksmoke16> oh wait
<FromGitter>
<Blacksmoke16> to be clear your example is fine and would work i think
<FromGitter>
<Blacksmoke16> the problem that PR is trying to address is sharing macro logic within other macros. In your case you're just using a macro to place down another macro within the generated code
<FromGitter>
<Blacksmoke16> unless at macro time that gets expanded to `String | Nil`
<FromGitter>
<evvo> It will be expanded, yes
<FromGitter>
<Blacksmoke16> 👍
<FromGitter>
<evvo> And that returns an Expression
<FromGitter>
<evvo> Hopefully no one creates a class with name `SomeNil`
<FromGitter>
<evvo> Maybe `.includes?("| Nil")` will work better
<FromGitter>
<Blacksmoke16> assuming its never `Nil | String`
<FromGitter>
<evvo> I see that usually Nil is the last in the list of unions
<FromGitter>
<evvo> I'm not sure if that is always the case ?
<FromGitter>
<Blacksmoke16> dunno
<FromGitter>
<evvo> I ended up returning the macros back, as there were more workarounds that I had to apply, which will make the code even more complex, instead of cleaning it :)
<FromGitter>
<Blacksmoke16> 👍
kerframil has joined #crystal-lang
ua has quit [Ping timeout: 260 seconds]
alexherbo2 has quit [Read error: Connection reset by peer]