RX14 changed the topic of #crystal-lang to: The Crystal programming language | http://crystal-lang.org | Crystal 0.27.1 | Fund Crystal's development: http://is.gd/X7PRtI | GH: https://github.com/crystal-lang/crystal | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/ | Gitter: https://gitter.im/crystal-lang/crystal
sz0 has joined #crystal-lang
<FromGitter> <iambudi> how to convert yml property number to string in a struct with YAML::Serializable?
<FromGitter> <iambudi> in yaml file i have `id: 1` and in crystal struct `property id : String` this throw error
<FromGitter> <iambudi> since yaml parses 1 as number rather than string
laaron has quit [Quit: ZNC 1.7.1 - https://znc.in]
laaron has joined #crystal-lang
<FromGitter> <Blacksmoke16> hm
<FromGitter> <Blacksmoke16> a custom converter prob would be way to go
<FromGitter> <Blacksmoke16> @iambudi
<FromGitter> <GabrielMorris> I'm running into an issue with Heroku - the dyno shuts down 60 seconds because a server doesn't get bound to the PORT env var, but the server is working locally and binding to it. https://github.com/GabrielMorris/crystallizer/blob/master/src/Crystallizer.cr
<FromGitter> <GabrielMorris> https://pastebin.com/WNKwnyFC
chemist69 has quit [Ping timeout: 240 seconds]
chemist69 has joined #crystal-lang
<FromGitter> <iambudi> @Blacksmoke16 Should it convert automatically based on the defined type. I use union as workaround.
<FromGitter> <Blacksmoke16> it wouldn't, thats the point of the converters
<FromGitter> <iambudi> How's the converter look like to convert number to string?
laaron has quit [Quit: ZNC 1.7.1 - https://znc.in]
laaron has joined #crystal-lang
<FromGitter> <Blacksmoke16> sec i can make an example
<FromGitter> <Blacksmoke16> having trouble with how to set it up but would be like https://play.crystal-lang.org/#/r/6gd4/edit
<FromGitter> <Blacksmoke16> going off example in stdlib https://github.com/crystal-lang/crystal/blob/master/src/yaml/from_yaml.cr#L255
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/6gd7 @iambudi here you go
<FromGitter> <iambudi> @Blacksmoke16 Nice. Thanks. It would be simpler if it has as notation field : `@[YAML::Field(as: String)]`
<FromGitter> <Blacksmoke16> :shrug:
<FromGitter> <iambudi> Another question is it possible to pass block to a method? `get "/" { |context, param| do_something }` to `get "/" a_method`
<FromGitter> <iambudi> `def a_method(context, param) ... end`
<FromGitter> <Blacksmoke16> dont think so?
<FromGitter> <iambudi> i want to separate it to make it cleaner, i thought it could.
<FromGitter> <Blacksmoke16> dunno, but if theres a way i dont know it
<FromGitter> <Blacksmoke16> wait maybe
<FromGitter> <iambudi> it gives clue, wondering how to pass the parameters
<FromGitter> <iambudi> need to find another way, can't figure it out now
<FromGitter> <pynixwang> https://github.com/pynixwang/baked_handler bundle statics to binary
<FromGitter> <pynixwang> oh, not work as a lib.
<FromGitter> <pynixwang> wait for upstream fix
<FromGitter> <pynixwang> works.
<FromGitter> <HarrisonB> I'm getting this error... ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c836d813162ec7bc742463a]
<FromGitter> <HarrisonB> The proc is ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c836d9e25e4e24c073d2891]
DTZUZO has quit [Ping timeout: 250 seconds]
crystal-lang564 has joined #crystal-lang
crystal-lang564 has quit [Quit: Konversation terminated!]
crystal-lang564 has joined #crystal-lang
crystal-lang564 has quit [Ping timeout: 258 seconds]
<FromGitter> <pynixwang> proc "->" change be move behand ()?
<FromGitter> <pynixwang> like js
<FromGitter> <pynixwang> (params) -> {body}
<FromGitter> <pynixwang> LLParser 's problem?
<FromGitter> <alex-lairan> There is a better way to get decimal part of a number ? ⏎ ⏎ ```icr(0.27.2) > val = 4.521 ⏎ => 4.521 ⏎ icr(0.27.2) > val % 1 ⏎ => 0.5209999999999999``` [https://gitter.im/crystal-lang/crystal?at=5c838bcff895944c08665588]
ashirase has quit [Ping timeout: 245 seconds]
ashirase has joined #crystal-lang
laaron has quit [Quit: ZNC 1.7.1 - https://znc.in]
laaron has joined #crystal-lang
<FromGitter> <r00ster91> How do I translate a C struct like this to Crystal? ⏎ ⏎ ```struct Book { ⏎ char title[50]; ⏎ char author[50]; ⏎ char subject[100]; ⏎ };``` ⏎ ⏎ I'm not sure about `[50]` etc. [https://gitter.im/crystal-lang/crystal?at=5c83988eac408e11922e7438]
<Yxhuvud> rooster: No idea, but I'm interested in the answer.
<FromGitter> <bew> @r00ster91 it means a static array of N, so in crystal: `title : LibC::Char[50]`
<FromGitter> <r00ster91> oh a static array.. that makes sense. Will try it, thanks
<FromGitter> <bew> In Crystal, `T[N]` in type notation is a shortcut for `StaticArray(T, N)`
<FromGitter> <r00ster91> yep it works. I no longer get an Invalid memory access
<FromGitter> <bew> @iambudi since Kemal's `get` is a macro I don't think you can properly forward a method proc..
<FromGitter> <bew> \o/
<FromGitter> <bew> @HarrisonB and how are you calling it? My best bet is that you're not giving it a json any type, but some other incompatible type
<FromGitter> <bew> You' need to change the types of the args you give i think
<FromGitter> <j8r> rkeene I don't see how to compile TCL as machine code, and how performant it is. Have you resources on this?
<FromGitter> <j8r> I only see the mention of Byte code in https://wiki.tcl-lang.org/page/Tcl+Performance
DTZUZO has joined #crystal-lang
<FromGitter> <j8r> @straight-shoota you can try to retrigger the CI for https://github.com/crystal-lang/crystal/pull/7420 , the image is now based on Crystal 0.27.2
Groogy has joined #crystal-lang
<Groogy> Yo! o/
<Yxhuvud> groogy: sup?
<Groogy> finally have time to play around with crystal again :D
<Groogy> been a while
<Yxhuvud> bew: hmm, does that work in normal classes too, so that if declare a static array as an instance variable, you get it allocated in the object itself without indirection?
<Groogy> It should, I use static array to define my vectors and use it with that assumption
<Groogy> think I checked ages ago and it did grow the size of the object
<FromGitter> <bew> Yeah it does Yxhuvud, hi Groogy :)
<Groogy> Hello
<Yxhuvud> nice, then I'll be able to solve an upcoming issue without resorting to overloading allocate.
_whitelogger has joined #crystal-lang
<FromGitter> <TheOnlyArtz> Hey, I didn't really get the idea of structs, when it is preferable to use them instead of classes?
<Groogy> I use them in cases where I want objects that are small on the stack or immutable
<Groogy> Like numbers are a struct because you never modify a number, you technically always replace it by the result of an operation
<Groogy> since they are by value and not by reference
<FromGitter> <TheOnlyArtz> Oh that makes more sense now
<FromGitter> <TheOnlyArtz> So I should use classes for mutable work?
<Groogy> well rather when you expect it to be by reference is probably better idea to think about
<Groogy> like obj.member.do_work
<Groogy> do you expect member to return a copy or the object itself?
<Groogy> if you expect it to be a copy it's a struct, if it's a reference to the object you should use classes
<FromGitter> <TheOnlyArtz> Ok so I will use class in my case
<FromGitter> <TheOnlyArtz> Thank you very much!
<Groogy> most probably you always want to use class :)
<FromGitter> <TheOnlyArtz> Do I really need the `property` keyboard?
<Groogy> property is a macro that generates the getter and setter methods for you
<Groogy> it's just a handy tool
<FromGitter> <TheOnlyArtz> But `getter` does that for me already?
<Groogy> ifyou want to be able to access the variable you need to
<Groogy> getter creates a getter, setter creates a setter and property creates both :P
<FromGitter> <TheOnlyArtz> oh lol
<FromGitter> <TheOnlyArtz> Nice
<FromGitter> <TheOnlyArtz> Does it matter if I use ⏎ `def initialize(@members : Hash(String, Player))` ⏎ or `def initialize(members : Hash(String, Player))`
<Yxhuvud> not if the body assigns to @members
<FromGitter> <TheOnlyArtz> What body?
<Yxhuvud> of initialize
<FromGitter> <TheOnlyArtz> I'm doing something like ⏎ ⏎ `````` [https://gitter.im/crystal-lang/crystal?at=5c83b27986e34a126fb163ff]
<FromGitter> <TheOnlyArtz> I'm doing something like ⏎ ⏎ `````` [https://gitter.im/crystal-lang/crystal?at=5c83b27d25e4e24c073ecdf4]
<Groogy> the first one automatically assigns it to the instance variable
<Groogy> while the second does not
<FromGitter> <TheOnlyArtz> Great thank you\
<Groogy> it also defines them ember variable if it wasn't previously defined
lucasb has joined #crystal-lang
laaron has quit [Remote host closed the connection]
<FromGitter> <TheOnlyArtz> What would be more ideal when defining a Hash type variable? ⏎ ⏎ ```@rooms = {} of String => Room ⏎ # vs ⏎ @rooms = Hash(String, Room)``` [https://gitter.im/crystal-lang/crystal?at=5c83b8c7e527821f0a2d0b05]
<Yxhuvud> The former is more ideal as it actually will initialize the hash. The latter assigns the class itself.
<FromGitter> <TheOnlyArtz> What's the former way?
<FromGitter> <TheOnlyArtz> `{} of ..`?
<Yxhuvud> Yes, though apart from using it correctly, I generally prefer the other way.
<FromGitter> <TheOnlyArtz> I like the former syntax then
laaron has joined #crystal-lang
<FromGitter> <TheOnlyArtz> I'm getting this error `unterminated macro` ⏎ Pointing at `macro assign(name, key, hash, value)`
<FromGitter> <TheOnlyArtz> ```code paste, see link``` ⏎ ⏎ This is the whole code (sorry if it looks messy, that's how a macro should look I suppose) [https://gitter.im/crystal-lang/crystal?at=5c83c087e527821f0a2d4489]
<FromGitter> <r00ster91> try removing the `do`
<FromGitter> <TheOnlyArtz> Oh yea
<FromGitter> <TheOnlyArtz> My bad
<FromGitter> <TheOnlyArtz> Is this macro looking fine or it's messier than it can be?
<FromGitter> <r00ster91> I would remove the spaces between the parentheses and the name and `assign`
<FromGitter> <r00ster91> otherwise it's fine
<FromGitter> <TheOnlyArtz> Done :)
<FromGitter> <TheOnlyArtz> I really like Crystal so far
<FromGitter> <TheOnlyArtz> When reviving a POST request, is there a way to modify the respond body? ⏎ All I see in the docs is how to write headers and reply with an error
<FromGitter> <Blacksmoke16> in kemal?
<FromGitter> <TheOnlyArtz> Yea
<FromGitter> <Blacksmoke16> `ctx.response.print`
<FromGitter> <TheOnlyArtz> Nice, thank you !
<FromGitter> <Blacksmoke16> response inherits from IO
<FromGitter> <TheOnlyArtz> I see...
<FromGitter> <TheOnlyArtz> When I try to read the body I'm getting HTTP::FixedLengthContent
<FromGitter> <TheOnlyArtz> And it's not really documented anywhere
<FromGitter> <Blacksmoke16> hm?
<FromGitter> <TheOnlyArtz> `puts ctx.request.body`
<FromGitter> <Blacksmoke16> its an io so try like `ctx.request.body.gets_to_end`
<FromGitter> <TheOnlyArtz> It fails with (compile-time type is IO | Nil)
<FromGitter> <Blacksmoke16> make sure there is a body first
<FromGitter> <TheOnlyArtz> ```if ctx.request.body ⏎ ... ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=5c83ceee25e4e24c073f831b]
<FromGitter> <Blacksmoke16> ```if body = ctx.request.body ⏎ puts body.gets_to_end ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=5c83cf6386e34a126fb22355]
<FromGitter> <TheOnlyArtz> Oh damn
<FromGitter> <TheOnlyArtz> ty :)
<FromGitter> <Blacksmoke16> np
<FromGitter> <TheOnlyArtz> ```code paste, see link``` ⏎ ⏎ I've learned a lot from here and it's nice to see it working [https://gitter.im/crystal-lang/crystal?at=5c83cfe5e527821f0a2da122]
<FromGitter> <Blacksmoke16> good to hear
<FromGitter> <Blacksmoke16> making a JSON api?
<FromGitter> <TheOnlyArtz> Nope, a type racer clone
<FromGitter> <TheOnlyArtz> https://github.com/TheOnlyArtz/racer
<FromGitter> <Blacksmoke16> JSON api for your type racer?*
<FromGitter> <TheOnlyArtz> No, it's a route for creating a new room for the players
<FromGitter> <Blacksmoke16> ah fair enough
<FromGitter> <TheOnlyArtz> How's my code so far? work-flow wise?
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c83d107bf7990126e763e62]
<FromGitter> <Blacksmoke16> thats redundant, could just do `getter id : String` etc
<FromGitter> <TheOnlyArtz> Oh really?
<FromGitter> <TheOnlyArtz> Nice to hear
<FromGitter> <TheOnlyArtz> "Fixed" :)
<FromGitter> <Blacksmoke16> nice
<FromGitter> <TheOnlyArtz> I got `router.cr` a class since I need to get some variables like the WS_Manager
<FromGitter> <TheOnlyArtz> ```code paste, see link``` ⏎ ⏎ So now the root looks like [https://gitter.im/crystal-lang/crystal?at=5c83d285d1e7281f09fefda8]
<FromGitter> <Blacksmoke16> `router.set_gets` what do these do?
<FromGitter> <TheOnlyArtz> I will show in a sec, I just need to fix something
<FromGitter> <TheOnlyArtz> It just fires up the routes
<FromGitter> <TheOnlyArtz> Not more than that
<FromGitter> <Blacksmoke16> prob easier to just have files that are related that you declare the routes in?
<FromGitter> <TheOnlyArtz> That's what I did
<FromGitter> <Blacksmoke16> like `room_controller.cr`?
<FromGitter> <Blacksmoke16> yea but there isnt really a reason to have them in a method
<FromGitter> <TheOnlyArtz> But I wanted to have access to the `manager`
<FromGitter> <Blacksmoke16> huh, ight yea that works
<FromGitter> <TheOnlyArtz> Now I have an issue
<FromGitter> <TheOnlyArtz> What makes Crystal think I'm about to give String or Int32 and not just String?
<FromGitter> <Blacksmoke16> id have to see the declaration of `@id`
<FromGitter> <Blacksmoke16> `@id = properties["id"]` since type of properties is `Hash(String, Int32 | String | Float64)`
<FromGitter> <Blacksmoke16> so since `properties` has a `Int32 and String` in it, the compiler cant know what key has what type
<FromGitter> <TheOnlyArtz> What can I do about it?
<FromGitter> <TheOnlyArtz> Can I explicitly state to the compiler that id will be a `String` just like I can explicitly do that with `Nil` with `not_nil!` ?
<FromGitter> <TheOnlyArtz> If you see a better way to construct Player I would love to here
<FromGitter> <bew> Either you cast the result to be String using `.as(String)` or maybe yiu could something else than a Hash which is not well suited to do what you want here I think
<FromGitter> <TheOnlyArtz> Then what will suite my needs, Bew?
<FromGitter> <TheOnlyArtz> NamedTuple?
<FromGitter> <bew> Directly pass id, and the other args to the Player constructor for exemple?
<FromGitter> <TheOnlyArtz> Ok I guess
<FromGitter> <TheOnlyArtz> I see it being weird to have a constructor with so many argument
<FromGitter> <bew> I don't see why you use a hash actually instead of creating the player with the values you have
<FromGitter> <TheOnlyArtz> You know more than me so I will just pass it straight away
<FromGitter> <bew> I don't have a magic solution to keep low nulber of params, sorry ^^
<FromGitter> <TheOnlyArtz> That's fine :)
<FromGitter> <TheOnlyArtz> That's how it goes with static typed languages I guess
<FromGitter> <bew> Or yeah a NamedTuple that you use in Player to get the values you need
<FromGitter> <bew> But for me that would be hiding some information where you don't really need to hide it..
<FromGitter> <TheOnlyArtz> Fair enough
<FromGitter> <TheOnlyArtz> Looks neat
<FromGitter> <TheOnlyArtz> I'm writing a class like ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ But when doing `Router::Helper.construct_player_play` it says this method is undefined? [https://gitter.im/crystal-lang/crystal?at=5c83dcea3162ec7bc7450359]
<FromGitter> <r00ster91> put `self.` in front of `construct_player_payload`
Yxhuvud has quit [Quit: No Ping reply in 180 seconds.]
<FromGitter> <TheOnlyArtz> Nice !
<FromGitter> <r00ster91> currently `construct_player_payload` is a class method which can only be called on instances of `Router::Helper` but making it `self.construct_player_payload` makes it a class method
<FromGitter> <TheOnlyArtz> I've looked at Crystal source code and didn't really see that `self.` before
<FromGitter> <TheOnlyArtz> Good to know
<FromGitter> <tenebrousedge> Ohai. I'm learning Crystal, and wondering if there is a "safe" way to extend core classes.
return0e_ has quit [Remote host closed the connection]
return0e has joined #crystal-lang
<FromGitter> <Blacksmoke16> like `MyClass < String`?
<FromGitter> <tenebrousedge> No, like changing `Int32` so that e.g. it has a `squared` method, but limiting this to a single file.
<FromGitter> <tenebrousedge> or otherwise limiting the scope of that change
<FromGitter> <Blacksmoke16> adding a method is easy enough, however scoping it to only a specific file might be a bit more tricky?
<FromGitter> <Blacksmoke16> you prob could use a macro to raise an exception if its used in a diff file than you want?
<FromGitter> <Blacksmoke16> unless someone else has a better idea that might be your best bet
<FromGitter> <Blacksmoke16> i can make an example in a bit if you need?
<FromGitter> <tenebrousedge> oh, don't trouble yourself. This is an academic point, really
<FromGitter> <TheOnlyArtz> Can I assign new properties to existing instances?
<FromGitter> <TheOnlyArtz> ```code paste, see link``` ⏎ ⏎ I want `socket` to hold the user ID, is it possible? [https://gitter.im/crystal-lang/crystal?at=5c83f58fd3b35423cb8fbeed]
daemonwrangler has quit [Quit: ZNC 1.6.5 - http://znc.in]
daemonwrangler has joined #crystal-lang
<FromGitter> <Blacksmoke16> hm
<FromGitter> <Blacksmoke16> like `socket.user_id = xxx`?
<FromGitter> <TheOnlyArtz> Yep
<FromGitter> <Blacksmoke16> prob not
<FromGitter> <TheOnlyArtz> I really don't want to have a class for that
<FromGitter> <Blacksmoke16> could use an orm with sqlite?
<FromGitter> <Blacksmoke16> for persisting data
<FromGitter> <TheOnlyArtz> Why tho?
<FromGitter> <TheOnlyArtz> I'm using arrays and hashes
<FromGitter> <TheOnlyArtz> Database is not needed here
<FromGitter> <TheOnlyArtz> It's all in real time
<FromGitter> <Blacksmoke16> fair enough
<FromGitter> <Blacksmoke16> then unless kemal provides a way to add custom data to it, prob not
<FromGitter> <TheOnlyArtz> Gotcha
<FromGitter> <Blacksmoke16> for `self.@ws_manager.connections.size == 0` could do `self.@ws_manager.connections.size.zero?`
<FromGitter> <TheOnlyArtz> alrighty
<FromGitter> <Blacksmoke16> little cleaner
<FromGitter> <TheOnlyArtz> indeed
<FromGitter> <bew> You should use `@foo` instead of `self.@foo`, the later is a not recommended syntax and usually not needed
<FromGitter> <TheOnlyArtz> Updated
<FromGitter> <TheOnlyArtz> Can't I have parameters with WebSockets?? oh no..
<FromGitter> <TheOnlyArtz> nvm I actually can. :)
<FromGitter> <franciscoadasme> hey guys, does anyone knows why the `#[]!` method is not a thing?, I'm using `#unsafe_fetch` in several places and I started to wonder about it (currently, `[]!` is invalid syntax)... As the bang method variants usually mean dangerous, I think `[]!` would be a perfect fit for index-based access without bounds check
<FromGitter> <Blacksmoke16> for an array?
<FromGitter> <r00ster91> In which places do you use `unsafe_fetch`?
<FromGitter> <r00ster91> LLVM is probably optimizing those places already so you don't have to use it.
<FromGitter> <franciscoadasme> in this case, yes, for an array... but my point is why use `#unsafe_fetch` instead of `#[]!`, which I think is more in-line with the naming conventions
<FromGitter> <r00ster91> I think `unsafe_fetch` shouldn't be renamed to `[]!` because it would make the unsafe method easier to use and would encourage more people to write unsafe code
<FromGitter> <franciscoadasme> I see... but that would apply to most bang methods to some extent, don't you think?
<FromGitter> <Blacksmoke16> what would you expect to happen if you used that and it was out of bounds?
<FromGitter> <Blacksmoke16> i dont see why you would want that and *not* have the bounds check?
<FromGitter> <franciscoadasme> Because sometimes you know the size of an array and you know is not going to change
<FromGitter> <r00ster91> so you use it for perfomance. I'm pretty sure the bounds check is actually *always* optimized out by LLVM and only left in when there will be an error
<FromGitter> <franciscoadasme> and doing such check hundreds or thousands of times will probably have a non-negligible performance impact
<FromGitter> <r00ster91> yep
<FromGitter> <franciscoadasme> ohh I didn't know that LLVM could be that "smart"
<FromGitter> <franciscoadasme> anyways, thanks for the answer
<FromGitter> <vladfaust> @r00ster91 I don't know what to rename it *to* instead of *how* :D
<FromGitter> <r00ster91> oh lol
marmotini_ has joined #crystal-lang
<FromGitter> <Blacksmoke16> what would you guy's ideal setup for cors, been thinking how i want to implement it
<FromGitter> <Blacksmoke16> like is there a use case for being able to override the settings on an endpoint by endpoint basis
<FromGitter> <Blacksmoke16> currently debating between a yaml config file like: https://github.com/nelmio/NelmioCorsBundle#configuration
<FromGitter> <Blacksmoke16> or like an in code setup like:
<FromGitter> <Blacksmoke16> ```Athena.config do |config| ⏎ config.allow.... = xxx ⏎ ... ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=5c842494d1e7281f090113c1]
<FromGitter> <Blacksmoke16> then an annotation to override those on a per endpoint basis
marius has joined #crystal-lang
<FromGitter> <chuckremes> anyone aware of recent articles on using crystal for the embedded space, eg micro controllers? last i saw was an article from bruce perens a year or two ago
marmotini_ has quit [Ping timeout: 250 seconds]
<FromGitter> <j8r> @Blacksmoke16 why not CON? https://github.com/j8r/con 😀
<FromGitter> <Blacksmoke16> ;)
<FromGitter> <j8r> usually headers are defined on the reverse proxy side, with from a dynamic configuration
<FromGitter> <Blacksmoke16> like env vars id imagine
<FromGitter> <Blacksmoke16> kinda leaning towards the config file. Would be more flexible and could have other options on it in the future...
<FromGitter> <Blacksmoke16> :shrug:
<FromGitter> <j8r> it may be possible to have both, if you serialize the file to a struct
<FromGitter> <Blacksmoke16> :thinking: then just have it so if an annotation is defined use that instead
<FromGitter> <Blacksmoke16> main thing i dont like about it is then id have to manage a file, which i could generate on shards install but hrm
<FromGitter> <Blacksmoke16> doing that then compile checks to make sure it exists should be pretty user friendly
<FromGitter> <Blacksmoke16> imma go with that and see how it goes, always iterate change when (if) if get feedback on it