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
fifr has quit [Ping timeout: 245 seconds]
fifr has joined #crystal-lang
Kixune has quit [Quit: Kixune]
S0bait has quit [Quit: Connection closed for inactivity]
sebasr has joined #crystal-lang
sebasr has quit []
szut has quit [Remote host closed the connection]
szut has joined #crystal-lang
chemist69 has quit [Ping timeout: 258 seconds]
chemist69 has joined #crystal-lang
return0e has joined #crystal-lang
return0e_ has quit [Ping timeout: 272 seconds]
<FromGitter> <dscottboggs_gitlab> @jwoertink yeah, I'm not a huge fan of the implementation of Time::Span either.
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
<FromGitter> <bew> There is a discussion around that in the forums iirc
<FromGitter> <dscottboggs_gitlab> oh cool I'll have to look that up
laaron has quit [Quit: ZNC 1.7.1 - https://znc.in]
laaron has joined #crystal-lang
johndescs has joined #crystal-lang
ashirase has quit [Ping timeout: 255 seconds]
ashirase has joined #crystal-lang
johndescs has quit [Ping timeout: 245 seconds]
johndescs has joined #crystal-lang
azuri5 has joined #crystal-lang
crystal-lang564 has joined #crystal-lang
<FromGitter> <bobbyPython> i tried the http server. How can I redirect the GET request / to my default index.html ?
<FromGitter> <bobbyPython> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c9379782fb6800d8057b33e]
<FromGitter> <Blacksmoke16> `response.headers.add "Location", url` is what it looks like?
<FromGitter> <Blacksmoke16> @bobbyPython
<FromGitter> <Blacksmoke16> with a 302 status_code
<FromGitter> <pynixwang> redirect_to
<FromGitter> <TheOnlyArtz> Hey guys
<FromGitter> <TheOnlyArtz> I'm writing a protocol in C with a friend
<FromGitter> <TheOnlyArtz> For a multiplayer game
<FromGitter> <TheOnlyArtz> Now, I want to link the library we are writing to Crystal server
<FromGitter> <TheOnlyArtz> Where's the docs for LibC?
<z64> LibC in std isn't a full binding for users. you can `lib LibC` and bind to whatever functions you need from libc's own documentation/headers etc
<FromGitter> <TheOnlyArtz> Let's say I got this function ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c9387eff3dbbd230c91d624]
<FromGitter> <TheOnlyArtz> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c938828f3dbbd230c91d7f5]
<FromGitter> <pynixwang> Void*?
<FromGitter> <TheOnlyArtz> I put `Pointer(Char)` there
<FromGitter> <TheOnlyArtz> But my main issue now is the `HandShakeClient`
<FromGitter> <TheOnlyArtz> how can Crystal understand that type?
szut has quit [Quit: Konversation terminated!]
Groogy has joined #crystal-lang
<FromGitter> <TheOnlyArtz> Well, I'm trying to put type `Enum` and it doesn't work
<FromGitter> <TheOnlyArtz> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c938d0bf3dbbd230c91f763]
<FromGitter> <ArtLinkov> Hey all, is there a built-in method to print comma separated numbers in strings (i.e print 1000 as 1,000)
<FromGitter> <r00ster91> I think https://github.com/crystal-lang/crystal/pull/6314 added that. But it won't be available until 0.28.0 is released.
<FromGitter> <ArtLinkov> Gotcha, thanks @r00ster91
<FromGitter> <Blacksmoke16> ah, beat me to it
<FromGitter> <Blacksmoke16> thought you could do it with `sprintf` as well but im thinking no
<FromGitter> <ArtLinkov> well, It's biggy... I'll just make a quick method of my own :)
<FromGitter> <straight-shoota> You can simply copy `humanize.cr` from master into your codebase and remove it once 0.28.0 is released
<FromGitter> <ArtLinkov> πŸ‘
<FromGitter> <TheOnlyArtz> I'm using UDPSocket API, every time I receive a packet the connection closes ( Well that's expected cause it's a stateless protocol ) ⏎ But whole the program stops itself
<FromGitter> <TheOnlyArtz> How can I prevent that from happening?
<FromGitter> <TheOnlyArtz> Put a loop around ⏎ `message, client_ip = server.receive` ?
<Groogy> Yo! o/
<FromGitter> <Blacksmoke16> o/
<FromGitter> <Naoyo> @TheOnlyArtz Yeah I just put a `loop do .. end` between `server.receive` and it worked.
azuri5 has quit [Quit: azuri5]
<FromGitter> <TheOnlyArtz> Nice
<FromGitter> <TheOnlyArtz> ty
azuri5 has joined #crystal-lang
<FromGitter> <TheOnlyArtz> ```code paste, see link``` ⏎ ⏎ Why does it fail? [https://gitter.im/crystal-lang/crystal?at=5c93a3d0d0133e21e5efdf1c]
azuri5 has quit [Quit: azuri5]
<FromGitter> <TheOnlyArtz> `expecting token ':', not 'initialize'`
azuri5 has joined #crystal-lang
<FromGitter> <TheOnlyArtz> I'm doing that inside `lib`
<Groogy> There are no intrusive profilers done yet for Crystal right?
<Groogy> need to figure out what in my perlin noise function is so slow
<z64> not specialized for crystal, no. you can use standard profilers like perf, valgrind etc
<FromGitter> <TheOnlyArtz> C bindings are killing me
<FromGitter> <TheOnlyArtz> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c93a76aa21ce51a2099a295]
<FromGitter> <straight-shoota> `"TEST".to_unsafe`
<FromGitter> <straight-shoota> Or copy the data from string to the ivar
<FromGitter> <straight-shoota> `tesa.displayName.value "5" # => runs` - that can't be true
<FromGitter> <TheOnlyArtz> Yea it's `5` don't know why I said `"5"`
<FromGitter> <z64> is there some reason you don't want to just pass a string
<FromGitter> <z64> seems overly complicated if you're just doing something like ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c93ab1d52c7a91455d0c9a1]
<FromGitter> <TheOnlyArtz> The C library expects and unsigned char
<FromGitter> <z64> yes, which are UInt8 values, like you're already passing
<FromGitter> <TheOnlyArtz> So using LibC::Char* will be suitable?
<FromGitter> <TheOnlyArtz> https://ghostbin.com/paste/3c42n
<FromGitter> <z64> i don't know about your C code. but what i just wrote above should be equivalent to what you presented before
<FromGitter> <z64> there's a bunch of notes about types in c bindings in <https://crystal-lang.org/reference/syntax_and_semantics/c_bindings/fun.html#types-in-c-bindings> that cover this kind of thing
azuri5 has quit [Quit: azuri5]
azuri5 has joined #crystal-lang
<FromGitter> <TheOnlyArtz> I've got an issue with the TCPServer API, I'm binding a new server to port 3000 and for some reason other machines can't access it while when I'm using Kemal, they can.
azuri5 has quit [Quit: azuri5]
<FromGitter> <TheOnlyArtz> I needed to use `open` instead of `new` issue solved
azuri5 has joined #crystal-lang
<FromGitter> <TheOnlyArtz> @straight-shoota I'm having issues with `Invalid memory access` ⏎ As you can see here I got crystal to the left and C to the right, I'm trying to call the `bufferToHandShakeClient` ⏎ and I'm getting this error
<FromGitter> <straight-shoota> I don't know what your code is doing, but these calls to fwrite, strcopy, cutstr seem to assume a certain string size which might not be true
<FromGitter> <TheOnlyArtz> Basically we are getting a buffer of bytes and slicing it by a certain offset to extract the data we want
<FromGitter> <TheOnlyArtz> We've put a ⏎ `printf("%s", client.displayName");` ⏎ right before the ⏎ `return client;` ⏎ in the function and got `"gguurr"` so the slicing did work. [https://gitter.im/crystal-lang/crystal?at=5c93c1baf3dbbd230c938764]
<FromGitter> <TheOnlyArtz> The issue seems to be with Crystal
<FromGitter> <straight-shoota> yes, the invalid memory access is definitely raised in Crystal
<FromGitter> <TheOnlyArtz> Well the issue is not in C, so what can raise such a thing in Crystal?
<FromGitter> <TheOnlyArtz> Do you want the crystal code?
<FromGitter> <straight-shoota> Where exactly does it raise?
<FromGitter> <TheOnlyArtz> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c93c28ff3dbbd230c938d32]
<FromGitter> <TheOnlyArtz> `puts typeof(tesa)` ⏎ runs tho
<FromGitter> <TheOnlyArtz> This is why it's so confusing
<FromGitter> <TheOnlyArtz> ```code paste, see link``` ⏎ ⏎ This is the result [https://gitter.im/crystal-lang/crystal?at=5c93c2d9dfc69a1454d07f3b]
<FromGitter> <straight-shoota> In your screenshot, the output contains `puts tesa`
<FromGitter> <TheOnlyArtz> We've changed the code a bit
<FromGitter> <TheOnlyArtz> It still raises the same error
<FromGitter> <straight-shoota> Does it print `puts tesa` as expected and raises afterwards?
<FromGitter> <TheOnlyArtz> `Protocol::HandShakeClient(@displayName=Pointer(UInt8)@0x727275756767, @clientVersion=Pointer(UInt8).null, @characterSecret=Pointer(UInt8).null)`
<FromGitter> <TheOnlyArtz> Yep, it prints and raises afterwards
<FromGitter> <straight-shoota> So how's `HandShakeClient` defined?
<FromGitter> <straight-shoota> Does it have a custom to_s or inspect method?
<FromGitter> <TheOnlyArtz> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c93c384a21ce51a209a703a]
<FromGitter> <straight-shoota> Okay, so the error is probably not from that instruction.
<FromGitter> <straight-shoota> To make sure, can you `puts` after `puts tesa`?
<FromGitter> <TheOnlyArtz> YEp
<FromGitter> <TheOnlyArtz> ``` puts tesa ⏎ puts 1 # => runs``` [https://gitter.im/crystal-lang/crystal?at=5c93c3c26a3d2e230d009700]
<FromGitter> <straight-shoota> Okay, so it's somewhere else
<FromGitter> <straight-shoota> Maybe the garbage collector thread...
<FromGitter> <TheOnlyArtz> Seems like we just can't work with Crystal on that one? :(
<FromGitter> <z64> fwiw, you don't need to call to_unsafe - when you pass an object into a c binding, to_unsafe is automatically called, see https://crystal-lang.org/reference/syntax_and_semantics/c_bindings/to_unsafe.html
<FromGitter> <TheOnlyArtz> sweet, but that won't help us resolve the memory issue
<FromGitter> <straight-shoota> I don't think this is a limitation of Crystal, it's probably just a stupid bug somewhere in your code. Probably related to some memory ownership issues between Crystal and C.
<FromGitter> <TheOnlyArtz> I mean... it's just a simple function
<FromGitter> <TheOnlyArtz> What bug can exist there exactly.. the C code works perfectly
<FromGitter> <straight-shoota> Dunno
<FromGitter> <straight-shoota> You said, removing `puts tesa` avoids the invalid memory access?
<FromGitter> <z64> a good place to start would be to validate what you are passing into the C function ⏎ is `message` what you expect it to be before passing it?
<FromGitter> <TheOnlyArtz> No, removing `tesa = Protocol.buffer....`
<FromGitter> <straight-shoota> Ofc, then it doesn't do anything
<FromGitter> <TheOnlyArtz> @z64 As I said, the C manipulates the Buffer perfectly
<FromGitter> <z64> i'm not skeptical of C. i'm asking you to check, i.e. `p message`, before passing it to the C function.
<FromGitter> <TheOnlyArtz> Well.. if the C function slices the buffer it gets, then yes `message` is a valid thing
<FromGitter> <straight-shoota> Can you show me the entire code?
<FromGitter> <TheOnlyArtz> Which?
<FromGitter> <straight-shoota> Crystal and C
<FromGitter> <TheOnlyArtz> https://ghostbin.com/paste/zhj2h
<FromGitter> <TheOnlyArtz> https://ghostbin.com/paste/6mk52
<FromGitter> <z64> it would just take a moment to try a simple sanity check of `p message` :p to me, it looks like you get a message that is parsed just fine. but, `IO#gets` is `String?` - the call to gets on the *next* iteration may return `nil`
<FromGitter> <TheOnlyArtz> You may be right
<FromGitter> <TheOnlyArtz> Nope, that's not the issue actually since it crashes without the `loop do ... end`
<FromGitter> <straight-shoota> Hm, there seems to be a discrepancy between your C and Crystal type definition
<FromGitter> <straight-shoota> Not sure if that's the issue
<FromGitter> <TheOnlyArtz> What do you mean?
<FromGitter> <straight-shoota> The instance variables of `HandShakeClient` are static arrays in C, an pointers in Crystal
<FromGitter> <TheOnlyArtz> Oh right
<FromGitter> <TheOnlyArtz> What's the equivalent of Static arrays then?
<FromGitter> <straight-shoota> `StaticArray(UInt8, 128)` etc.
<FromGitter> <TheOnlyArtz> OMG
<FromGitter> <TheOnlyArtz> Thank you very much!
<FromGitter> <straight-shoota> Or just `UInt8[128]`
<FromGitter> <straight-shoota> πŸ‘
<FromGitter> <TheOnlyArtz> How would I be able to convert all of that to a String
<FromGitter> <j8r> `String.new(StaticArray)`?
<FromGitter> <straight-shoota> `String.new(client.displayName.to_slice)`
<FromGitter> <j8r> Yes that's to_slice
<FromGitter> <TheOnlyArtz> That's fantastic guys! you're the best
<FromGitter> <straight-shoota> Great that it helped
<FromGitter> <j8r> what's the main difference between StaticArray and Slice?
<FromGitter> <bew> basically, a slice is a pointer and a size ; a static array is a bunch of bytes on the stack or embedded in an object (like a struct) and you manipulate these bytes like an array where the size can't change (known at compile time)
lucasb has joined #crystal-lang
Jenz has joined #crystal-lang
gangstacat has quit [Ping timeout: 252 seconds]
<FromGitter> <TheOnlyArtz> `StaticArray(UInt8, 16).new 0`
<FromGitter> <TheOnlyArtz> How come this is not valid?
<FromGitter> <TheOnlyArtz> `no overload matches 'StaticArray(UInt8, 16).new' with types Int32, characterData: StaticArray(UInt8, 492)`
<FromGitter> <Blacksmoke16> try ``StaticArray(UInt8, 16_u8).new 0`
<FromGitter> <Blacksmoke16> default type for numbers w/o suffix is `Int32`
<FromGitter> <TheOnlyArtz> `Syntax error in server.cr:34: expecting token ')', not '16'`
<FromGitter> <TheOnlyArtz> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c93ddb8f3dbbd230c9458d4]
Jenz has quit [Ping timeout: 244 seconds]
<FromGitter> <kinxer> Why would you need the `16` and `492` to be `UInt8`?
<FromGitter> <kinxer> Also, https://carc.in/#/r/6jgg
<FromGitter> <kinxer> Wait, wasn't there recently a change that auto-casts certain literals? What version of Crystal are you using?
<FromGitter> <TheOnlyArtz> latest
<FromGitter> <Blacksmoke16> ah my bad, put suffix on wrong value
<FromGitter> <kinxer> I'm having difficulty replicating your error, @TheOnlyArtz: https://carc.in/#/r/6jgn
<FromGitter> <kinxer> Still having trouble: https://carc.in/#/r/6jgs
<FromGitter> <kinxer> I copied the wrong thing... Is this your error: https://carc.in/#/r/6jgv ?
<FromGitter> <kinxer> If so, the whole error is ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ That's an error where the method arguments after `serverVersion: StaticArray(UInt8, 16_u8).new 0` are being interpreted as `StaticArray(T,N).new` arguments, which can be fixed by adding parentheses: https://carc.in/#/r/6jgw [https://gitter.im/crystal-lang/crystal?at=5c93e5a79d9cc8114afbbecd]
<FromGitter> <johnjansen> for consistency, probably add the brackets to both β€œnew”
<FromGitter> <kinxer> Yeah, definitely. I left off the second set of parentheses to emphasize that they're not necessary, though.
<FromGitter> <johnjansen> totally ;-)
<robacarp> burn the unnecessary syntax
<FromGitter> <johnjansen> IMO in this case ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ bracketing the 0 on the last line is necessary for consistency … given the previous one is, and the closing bracket has the potential to add confusion [https://gitter.im/crystal-lang/crystal?at=5c93ed125547f774484244b0]
<FromGitter> <kinxer> Yes, I agree that it's necessary, as much as proper style and readability make something necessary. I specifically left off the parentheses for the second `StaticArray` constructor to emphasize that the parentheses on the first constructor are the *syntactically* necessary addition.
<FromGitter> <johnjansen> i wasnt aiming the comment at you @kinxer
<FromGitter> <kinxer> Oh, okay. My bad.
<FromGitter> <johnjansen> no bad ;-) all good
<FromGitter> <kinxer> My preferred style is actually ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c93edf2dfc69a1454d1bafe]
<FromGitter> <johnjansen> its was in response to `*<robacarp>* burn the unnecessary syntax`
<FromGitter> <kinxer> Ah... I assumed that was a joke. :P
<FromGitter> <johnjansen> hard to know
<robacarp> there are varying degrees of necessity, some are compiler driven and some are readability driven
<robacarp> but for example, I know a bunch of javascript devs who insist on using semicolons everywhere just because, yet it's not helpful to the interpreter (most of the time) and it's not really going to help readability
<FromGitter> <TheOnlyArtz> Guys, I'm using TCPServer, whenever a message is coming from the client, I won't be able to read it until the client connection is getting closed
<FromGitter> <TheOnlyArtz> How come?
<FromGitter> <TheOnlyArtz> Basically I have to do something like ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5c93f7db6a3d2e230d021b7d]
<FromGitter> <TheOnlyArtz> Why can't I read the packet and only then send a packet of my own?
<robacarp> I think by default gets reads to newline characters, are you sending complete lines?
<FromGitter> <TheOnlyArtz> I'm sending buffers
<FromGitter> <TheOnlyArtz> Like huge arrays
<robacarp> then I'd use something other than gets
<FromGitter> <TheOnlyArtz> Like
<robacarp> this is reading from STDIN, but it's the same IO parent class
<FromGitter> <TheOnlyArtz> uh? how would I use it on the snippet I sent :?
<robacarp> you need to create a buffer `buf = Bytes.new BUFFER_SIZE` and then call #read(buf) instead of #gets
<robacarp> then iterate the buffer and do what you need with the data
<FromGitter> <TheOnlyArtz> I've actually tried this one
<FromGitter> <TheOnlyArtz> I'm getting the length (I think)
<FromGitter> <TheOnlyArtz> Yes, I'm getting the length of the message
<Groogy> so this is weird https://play.crystal-lang.org/#/r/6jhq
<Groogy> I expected that to count as explicitly counting as providing the generic argument
<Groogy> it works perfectly fine within a method
gangstacat has joined #crystal-lang
<z64> the metaclass isn't instantiated for generics like that. T can't be inferred by looking at the class definition alone
<Groogy> Yeah trying to optimize so I don't have to new everytime the method is called
<Groogy> since value will always be the same but because the generic argument is not available I can't
<Groogy> anyone got any ideas how I can accomplish that? I.e avoid an allocation there?
<Yxhuvud> I suppose you actually have more code in there and isn't simply trying to bend the type system into a lookup table by class?
<Groogy> Well it's for my vectors
<Groogy> and it's normal to want to fetch out zero and unit(1) vectors
<Groogy> so it gets called all over the place so instead of having to new each time I might as well just have a copy already defined
<Groogy> the Vector.zero method appears a lot in the profiling data
<Yxhuvud> How about a constant instead of a class variable?
laaron- has joined #crystal-lang
laaron has quit [Remote host closed the connection]
<Yxhuvud> Hmm. Seems to be the T.new part that doesn't work.
<Yxhuvud> (for the constant case)
azuri5 has quit [Ping timeout: 252 seconds]
<Yxhuvud> Do the vectors have more content? it seems very strange that a small *struct* spend a noticable time being allocated considering that they are allocated on the stack and copied by value.
<Groogy> no it's just used in a lot of places
<Groogy> @pos = Boleite::Vector2f.zero
<Groogy> and in algorithms etc.
<Groogy> it's a bit of micro optimization but would be gone to get gone
<Groogy> been fixing other hotspots meanwhile
<Groogy> good to get it gone*
Kixune has joined #crystal-lang
hightower2 has joined #crystal-lang
<hightower2> Hey is Time#epoch_ms now called Time#to_unix_ms or what?
<FromGitter> <Blacksmoke16> yes
<hightower2> alrighty
<hightower2> thanks
<FromGitter> <mikeonslow> Hey all, can't say that I've used Crystal but I really the ideas behind it! We're hosting a new conference and are looking for speakers. We'd love to hear from the Crystal community: https://www.papercall.io/dtw, event details here: https://detroittechwatch.org/
Kixune1 has joined #crystal-lang
<hightower2> mikeonslow: we glad you really the ideas! :)
hightower2 has quit [Quit: Leaving]
Kixune has quit [Ping timeout: 272 seconds]
Kixune1 is now known as Kixune
<FromGitter> <mikeonslow> Definitely! I think it solves some common pain points for sure. We moved from using PHP to Elixir about a year and a half ago to solve some of those pain points. Wish we had the static type checking in the core of the language though like Crystal does!
Kixune has quit [Ping timeout: 252 seconds]
Kixune has joined #crystal-lang
Kixune1 has joined #crystal-lang
Kixune1 has quit [Client Quit]
Kixune1 has joined #crystal-lang
Kixune has quit [Ping timeout: 268 seconds]
Kixune1 is now known as Kixune