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
alexherbo24 has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 240 seconds]
alexherbo24 is now known as alexherbo2
deavmi has quit [Read error: Connection reset by peer]
deavmi has joined #crystal-lang
chachasmooth has quit [Ping timeout: 256 seconds]
chachasmooth has joined #crystal-lang
<FromGitter> <mixflame> where can we write that if anyone's using dart with crystal they need strict_encode
<FromGitter> <mixflame> it should probably be written somewhere
<FromGitter> <mixflame> i know its anecdotal
<FromGitter> <Blacksmoke16> prob a blog post or something
<FromGitter> <mixflame> but was a pretty hard bug to fight today
<FromGitter> <mixflame> I need to start a crystal blog honestly
<FromGitter> <mixflame> it would be cool if there was a crystal community blog, made in crystal that trusted people could post on
alexherbo2 has quit [Remote host closed the connection]
<postmodern> mixflame, stackoverflow post, if you have the necessary karma
<FromGitter> <mixflame> didn't know you could make posts that weren't questions
<FromGitter> <mixflame> i have good karma
<FromGitter> <mixflame> hope this removes the roadblock for future developers
<postmodern> i've seen people post things that were just basically TIL posts. Well they may have edited it afterwards.
chillfox has joined #crystal-lang
<FromGitter> <mixflame> ahh good idea
<jhass> oprypin: heh, whatever works I guess
<FromGitter> <naqvis> Hi jhass, nice to see you around :P
<FromGitter> <naqvis> haven't seen you around since quite some time, hope things are well
<jhass> sure, was just spending time more outside while it was still warm over here :)
<FromGitter> <naqvis> nice
<FromGitter> <guglielmocg_gitlab> Pardon me for the noob question, but when I want to create an array of JSON with JSON::ArrayBuilder, what is the T parameter? I can't help finding the doc a bit cryptic about this
<FromGitter> <guglielmocg_gitlab> @mixflame that blog idea would be a great thing
<jhass> there's no JSON::ArrayBuilder?
<jhass> might be easier to show what kind of data you have and what the generated JSON should look like
<jhass> JSON is not a datatype in Crystal, so you cannot have an "array of JSON"
r0bby has quit [Ping timeout: 272 seconds]
r0bby has joined #crystal-lang
<FromGitter> <j8r> but you can have an array of JSON::Any
<jhass> sure but that's what they got would be a pure assumption, better to ask :)
<FromGitter> <guglielmocg_gitlab> The response to HTTP::Client.get("#{base_url}", headers: HTTP::Headers{"Accept" => "application/json", "api-key" => "#{ENV["SIB_API"]}"}) is ⏎ { ⏎ ⏎ ```code paste, see link``` ⏎ ... [https://gitter.im/crystal-lang/crystal?at=5f46291b4823780937437e86]
<FromGitter> <guglielmocg_gitlab> I can have it as a string by going ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f46295ba5788a3c29b2f89c]
<FromGitter> <guglielmocg_gitlab> But I need, ideally, to have a Hash, or to serialize it into a JSON (or JSON::Any). I believe this would need two nested serializers since the structure is ⏎ "items" { "events"[ "email": "", "date": "", "subject": "" ... ] }
<jhass> you seem a little confused about terminology. JSON is a serialzation format. The string representation is the serialized form. Going from string to datastructure is called deserialization
<jhass> I take it you have well structured data from some API endpoint. I'm not sure what you endgoal with that data is, because you described none of your goals, but I would guess the cleanest code is if you were to build a couple of JSON::Serializable based structs
<FromGitter> <guglielmocg_gitlab> This is entirely possible since I'm new to the industry and mostly worked in Ruby. I'm used to a much less complex type system.
<jhass> the terminology remark is entirely independent of any language or type system, it's a rather abstract concept
<FromGitter> <guglielmocg_gitlab> I admit I have much to learn, but I can't get around the fact that ⏎ pp! Hash(String, Lucky::Response).new(HTTP::Client.get("#{base_url}", headers: HTTP::Headers{"Accept" => "application/json", "api-key" => "#{ENV["SIB_API"]}"})) ⏎ makes the compiler go: ⏎ In lib/lucky/src/lucky/renderable.cr:124:5 ⏎ web | ... [https://gitter.im/crystal-lang/crystal?at=5f462b9edfaaed4ef50e2d0c]
<FromGitter> <guglielmocg_gitlab> It's what I'm gonna do but it's gonna be quite bloated, unless it is possible to nest JSON::Serializable structs. Is it?
<jhass> you could do something like this: https://carc.in/#/r/9lc1
<jhass> again, I have no clue as to what your endgoal is
<FromGitter> <guglielmocg_gitlab> sorry for not being clear. My goal is to take the result of the request and turn it ideally into a hash
<FromGitter> <guglielmocg_gitlab> or have it as a hash
<FromGitter> <guglielmocg_gitlab> A JSON::Any would also do
<FromGitter> <j8r> `JSON::Any` should only be used if you don't know the mapping at all
<FromGitter> <j8r> otherwise using proper types will be better
<FromGitter> <guglielmocg_gitlab> @j8r so something like JSON(String, String).new?
<FromGitter> <j8r> I mean, to what extend you know the mapping? It is just a Hash of "random" keys, values?
<jhass> "turn it into a hash" is not your goal, it's the solution towards your goal that you envision. Classic XY question :)
<FromGitter> <j8r> What jhass showed can also be applied for a request
<FromGitter> <guglielmocg_gitlab> Oh I get the misunderstanding! I need to return ordered, useable data from this request to my esteemed frontend colleague
<jhass> so you showed an example of the source data, how would the response based on that look like?
<FromGitter> <guglielmocg_gitlab> Oh shoot @jhass I had missed the carc.in link you sent me. That looks really good
<FromGitter> <guglielmocg_gitlab> That shows me how to nest serializables. This is so good. I tried it but my syntax was not good.
<jhass> so if it's just about sorting the events array, you can do that and use .to_json anywhere to serialize it back into JSON (it optionally takes an IO argument for streaming response)
<jhass> if you're remapping, I'd make a second set of structs and give them constructors that take the first set of structs
<FromGitter> <guglielmocg_gitlab> I'm still getting my head around IO arguments for JSON
postmodern has quit [Quit: Leaving]
zorp_ has joined #crystal-lang
duane has quit [Ping timeout: 240 seconds]
[gnubie] has joined #crystal-lang
[gnubie] has quit [Quit: Colloquy for iPhone - http://colloquy.mobi]
<FromGitter> <mwlang> No worries, @wyhaines, I got it sorted. The whole intent was to understand crystal better and esp. when what I write in Ruby doesn't quite translate to crystal and this was a good exercise to that end.
<FromGitter> <mwlang> I like that last version you came up with. no idea why I didn't think to tack `#map` on to the outside of the block.
[gnubie] has joined #crystal-lang
<FromGitter> <j8r> @guglielmocg_gitlab in fact, you can pass either a String or any IO
alexherbo2 has joined #crystal-lang
[gnubie] has quit [Ping timeout: 256 seconds]
duane has joined #crystal-lang
duane has quit [Read error: Connection reset by peer]
duane has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 260 seconds]
alexherbo2 has joined #crystal-lang
alexherbo23 has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 260 seconds]
alexherbo23 is now known as alexherbo2
andremedeiros has quit [Quit: ZNC 1.8.1 - https://znc.in]
andremedeiros has joined #crystal-lang
duane has quit [Ping timeout: 260 seconds]
ua has quit [Excess Flood]
ua has joined #crystal-lang
chillfox has quit [Ping timeout: 240 seconds]
<Andriamanitra> probably a stupid question but why/how does this work https://play.crystal-lang.org/#/r/9le5
zorp_ has quit [Ping timeout: 256 seconds]
<Andriamanitra> and i can even make the chain as long as i want and it still just seems to work
alexherbo2 has quit [Ping timeout: 258 seconds]
<FromGitter> <Blacksmoke16> prob because in the other 3 examples you're comparing a `Bool` with a `Int32`
<FromGitter> <Blacksmoke16> which aren't comparable
<Andriamanitra> yes, i get why those don't work, what my question is why the first one DOES work
<Andriamanitra> in ruby "1 < 2 < 3" would result in "NoMethodError: undefined method `<' for true:TrueClass"
<FromGitter> <Blacksmoke16> :shrug: compiler is just smart enough to see they're all numbers
<FromGitter> <Blacksmoke16> is what i would say
alexherbo2 has joined #crystal-lang
<FromGitter> <Blacksmoke16> either that or it just gets optimized out
<FromGitter> <dscottboggs> no this is a feature that is not in ruby that is in crystal
<Andriamanitra> there must be some kind of special case where the compiler checks if there's more comparison operators after the first one but how does that even work if i have a custom #< method
<FromGitter> <dscottboggs> putting the operators next to each other follows python-like semantics in Crystal
<FromGitter> <dscottboggs> the only exception being that obviously Crystal has no `in` operator like Python does (instead we use the `#includes?` method)
<FromGitter> <Blacksmoke16> there is `in?`
<FromGitter> <dscottboggs> oh I didn't know that, nice
<FromGitter> <dscottboggs> wait, is that an operator or method?
<FromGitter> <Blacksmoke16> https://crystal-lang.org/api/master/Object.html#in?(*values:Object):Bool-instance-method
<FromGitter> <Blacksmoke16> method
<FromGitter> <dscottboggs> oh cool
<FromGitter> <Blacksmoke16> also usually you can do `===` for that
<FromGitter> <dscottboggs> really?? Nice!
<FromGitter> <Blacksmoke16> like `1 === 0..10 # => true`
alexherbo2 has quit [Ping timeout: 264 seconds]
<FromGitter> <Blacksmoke16> er maybe not
<FromGitter> <Blacksmoke16> `(0..10) === 1` this way
<FromGitter> <Blacksmoke16> as its an overload of `Range` for example
<FromGitter> <dscottboggs> my point is that python has a `in` operator which has some weird semantics around operator *chaining* like in the example. I think I saw like `False == True in [False]` or something like that has a weird result in python. that wouldn't happen in Crystal because the equivalent would be `false == true.in? [false]` or `(false == true).in? [false]` which is more obvious
alexherbo2 has joined #crystal-lang
<FromGitter> <3n-k1> is there a way to define a custom operator in crystal?
<FromGitter> <Blacksmoke16> no
<FromGitter> <dscottboggs> https://play.crystal-lang.org/#/r/9lem ?
<FromGitter> <3n-k1> probably for the best, but there goes that plan :P
<Andriamanitra> i would have expected error because there's no comparison operator between Int32 and MyNum but apparently the result is 2
<FromGitter> <dscottboggs> @3n-k1 yes only the default operators are allowed.
<FromGitter> <dscottboggs> would be nice if there were a few more of them though
<FromGitter> <Blacksmoke16> but you're not comparing numbers, you're comparing `MyNum` instances
<FromGitter> <Blacksmoke16> add a `< 10` and see
<Andriamanitra> Blacksmoke16 my #< method returns int32, not true or false
<FromGitter> <Blacksmoke16> which is always true
<FromGitter> <Blacksmoke16> you should look into https://crystal-lang.org/api/master/Comparable.html
<Andriamanitra> yeah but "true < MyNum" doesn't make any more sense than "Int32 < MyNum"
<FromGitter> <Blacksmoke16> true
<Andriamanitra> but MyNum doesn't implement comparable so that shouldn't come into play
<FromGitter> <Blacksmoke16> i just meant in general if you're needing comparison stuff like this
<Andriamanitra> oh i'm not actually trying to achieve anything i just got curious how the language can possibly handle this
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/9ler
<FromGitter> <Blacksmoke16> same magic it does for the first example
alexherbo2 has quit [Ping timeout: 240 seconds]
alexherbo2 has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 258 seconds]
duane has joined #crystal-lang
alexherbo2 has joined #crystal-lang
<Andriamanitra> i guess the moral of the story is if you define comparison methods in a silly way, expect them to behave in silly ways too https://play.crystal-lang.org/#/r/9lfk
alexherbo2 has quit [Ping timeout: 264 seconds]