<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?
<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))`
<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> `@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>
<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?
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> 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