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
guniberas has joined #crystal-lang
notzmv has quit [Ping timeout: 260 seconds]
guniberas has quit [Ping timeout: 264 seconds]
notzmv has joined #crystal-lang
casionaut has joined #crystal-lang
casionaut has quit [Remote host closed the connection]
Guest68 has joined #crystal-lang
<Guest68> i am going through the docs and i don't understand what forall is `def foo(x : T) forall T`? is it just the same as generics except for free functions (not inside class)? or is there some difference/special use case? if it is the same as generics then why have this separate syntax?
<Guest68> and the docs use the term "free variable", what exactly does that mean?
<FromGitter> <Blacksmoke16> you can kinda think of it like generics but for functions
<FromGitter> <Blacksmoke16> like `foo 10` and `foo "foo"` `T` would be `Int32` and `String` respectively
<FromGitter> <Blacksmoke16> they can also be used to extract the type of containers
<FromGitter> <Blacksmoke16> ```def foo(x : Array(T)) forall T ⏎ T ⏎ end ⏎ ⏎ foo [1,2,3] # => Int32``` [https://gitter.im/crystal-lang/crystal?at=617eb5e5cd4972068b7d6d48]
<FromGitter> <Blacksmoke16> and because `T` is known at compile time, you can use it within macro logic
<Guest68> then why have 2 different syntaxes. the existing bracket syntax `class ABC(T)` could have been used, or a different bracket could be used like nim uses `[]`
<Guest68> anyways i just wanted to know whether it is normal generic or something else
<FromGitter> <Blacksmoke16> because one is a generic on a type and the other is on amethod
<Guest68> so that i know now
<FromGitter> <Blacksmoke16> you can intermix the two
Guest68 has quit [Quit: Client closed]
<FromGitter> <Blacksmoke16> but to answer you question, no freevars are not the same thing as generics
<FromGitter> <Blacksmoke16> but are similar concept wise
<SamantazFox> `Exception: Expected Hash for #[]?(key : String), not String (Exception)`
<SamantazFox> I don't understand that error
<FromGitter> <Blacksmoke16> Got the code that reproduces it?
<SamantazFox> exact commit, from the error provided by the software, but I don't get why `String (Exception)` shows up. If there are any error, the `.try` statements would catch it.
<SamantazFox> And I've double checked: the input data (JSON, from youtube) is perfectly valid.
<SamantazFox> Here is the code extract: https://play.crystal-lang.org/#/r/c6f7
dmgk has joined #crystal-lang
<FromGitter> <naqvis> can you provide the exception you are getting?
<FromGitter> <naqvis> but looking at code, i would say `as_a` is a hard condition which might not be getting satisfied
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/c6fh
<FromGitter> <Blacksmoke16> are using `#[]` on a value thats a string
<FromGitter> <Blacksmoke16> er `#[]?` to be specific
<SamantazFox> naqvis:https://github.com/iv-org/invidious/issues/2549
<SamantazFox> check the "backtrace" (needs to be expanded)
<SamantazFox> Blacksmoke16: Ok, I got why.
<SamantazFox> Thanks y'all
<FromGitter> <Blacksmoke16> 👍
<SamantazFox> The problem is that I have an edge case that causes the code supposed to catch error messages returned by the API that doesn't catch said error
<FromGitter> <Blacksmoke16> prob would really help to prioritize getting some spec coverage on this stuff
<SamantazFox> The problem we have is that the content returned by YT changes often
<SamantazFox> So one day, everything works, and the next day, one video is copyrighted, or removed, or they change the protobuf object for the search parameters, or whatever
<FromGitter> <Blacksmoke16> ah true, sounds like a pita :p
<SamantazFox> yeaaah xD
<SamantazFox> it's a constant mice and cat game
hightower2 has joined #crystal-lang
notzmv has quit [Read error: Connection reset by peer]
notzmv has joined #crystal-lang
ur5us has joined #crystal-lang
<FromGitter> ... messy.
<FromGitter> <christopherzimmerman> Has anyone ever dealt with using a Singleton pattern alongside generics? I have a class where I want to only instantiate an instance of each possible type once, since it allocates some C objects that really need to be re-used. Something like this (obviously this doesn't work): https://play.crystal-lang.org/#/r/c6fy. I'd prefer not to have to use the second approach and make the code a lot more
<FromGitter> <Blacksmoke16> hmm, what about like
<FromGitter> <Blacksmoke16> i think you're going to need a specific class for each, since a generic type is only related to an instantiation of a type
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=617eff5ba41fd2069917ae0f]
<FromGitter> <Blacksmoke16> oh wait, new idea
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=617f00843f09d8573647ec79]
<FromGitter> <Blacksmoke16> something like that? :shrug:
<FromGitter> <Blacksmoke16> cant have a hash key of type `Object.class` so i just used a string. Probably be fine for primitive types, but could get weird if you need to support unions or array of unions etc
<FromGitter> <christopherzimmerman> @Blacksmoke16 that second approach is so close to what I want, think I'm going to run into an issue adding generic funcs to it though: https://play.crystal-lang.org/#/r/c6g6
<FromGitter> <christopherzimmerman> I think the first approach will work fine though
<FromGitter> <Blacksmoke16> yea that'd prob be your best bet. At least should be a bit more robust
<FromGitter> <Blacksmoke16> could use a macro to generate the subclasses
<FromGitter> <christopherzimmerman> Oh I hope so lol, since there are about 850 of them :P
<FromGitter> <Blacksmoke16> :S
<FromGitter> <christopherzimmerman> Is the inherited macro just so it returns an instance of the subclass and not the parent? Haven't used that before
<FromGitter> <Blacksmoke16> was to keep all the related logic in the parent
<FromGitter> <Blacksmoke16> might also want to include the private initializer in there too
<FromGitter> <Blacksmoke16> would be same as if you manually added it to every child
hightower2 has quit [Ping timeout: 260 seconds]
oprypin_ has quit [Quit: Bye]
oprypin has joined #crystal-lang