<FromGitter>
<aisrael> Is there a way to use `JSON.mapping()` with a custom initializer? I’m gettin `this 'initialize' doesn't explicitly initialize instance variable ‘@foo' of Bar, rendering it nilable`
Totoro has quit [Ping timeout: 248 seconds]
Totoro has joined #crystal-lang
duane has quit [Ping timeout: 255 seconds]
alex`` has joined #crystal-lang
<FromGitter>
<bew> well, `JSON.mapping` generate an `initialize` method, if you have additional fields that is not in the json mapping, you can initialize them in the type declaration (e.g: `@foo = some_value`), else it's not possible
<FromGitter>
<aisrael> TIL ☝️ Thanks
<FromGitter>
<bew> Yeah, I don't think it's possible for more complex ivars, not with tha json mapping macro at least. You can always do without it, but it's a lil' bit longer ˆˆ
HoloIRCUser has joined #crystal-lang
flaviodesousa has joined #crystal-lang
<FromGitter>
<aisrael> Can a macro capture a block? :D Am wondering if `JSON.mapping(…) do # custom initialisation code` would work
HoloIRCUser has quit [Ping timeout: 248 seconds]
<FromGitter>
<bew> Yes could work, not sure it has been ever suggested 👍
<FromGitter>
<xfbs> Given a path to a base directory, and a path to a file in that directory, how can I get a relative path from the base directory to the file?
<lvmbdv>
i keep refreshing but nothing yet sdogruyol
<lvmbdv>
it's only morning in US :^)
<FromGitter>
<sdogruyol> let's hope for the best
maattdd has quit [Ping timeout: 248 seconds]
TakeYourFreedom has quit [Ping timeout: 248 seconds]
bijan_ has joined #crystal-lang
DTZUZO has joined #crystal-lang
maattdd has joined #crystal-lang
rohitpaulk has joined #crystal-lang
maattdd has quit [Ping timeout: 252 seconds]
HoloIRCUser2 has joined #crystal-lang
maattdd has joined #crystal-lang
rohitpaulk has quit [Ping timeout: 260 seconds]
maattdd has quit [Ping timeout: 264 seconds]
alex`` has quit [Quit: WeeChat 2.0.1]
alex`` has joined #crystal-lang
HoloIRCUser2 has quit [Ping timeout: 248 seconds]
duane has joined #crystal-lang
bijan_ is now known as bijan_awaaaay
TakeYourFreedom has joined #crystal-lang
duane has quit [Ping timeout: 260 seconds]
<FromGitter>
<hfabre> Hi guys, i'm new to crystal and i wonder why i keep getting a: `in src/crplat/commands/leave_command.cr:12: undefined method 'remove_user' for Nil (compile-time type is (Crplat::Channel | Nil))`
<FromGitter>
<hfabre> any idea of what i'm doing wrong ?
<hightower2>
hfabre: crystal determines all possible types for a variable at compile time. When you call some method on an object, that method must exist on all types. In your case, Crystal has identified that your variable can either be Crplat::Channel or Nil, and there is no method "remove_user" defined on Nil
<FromGitter>
<hfabre> Yes but i have an explicit `unless @channel.nil?` on the line before, shoud'nt it be sufficient ?
<hightower2>
Yes but since this is a property (instance variable) something could change it outside of your code
<hightower2>
you should redesign it to be like this:
<hightower2>
channel = @channel.not_nil!
<hightower2>
channel.remove_user
<hightower2>
(i.e. you make a local var, then work on the local var)
<FromGitter>
<yxhuvud> I prefer `if channel = @channel` ..
<FromGitter>
<hfabre> 😮 didn't know about this thanks !
<hightower2>
right, sure... was just giving a general example
<FromGitter>
<hfabre> works well thanks :)
bijan_awaaaay is now known as bijan_
<FromGitter>
<aisrael> TIL: `var.not_nil!` for unbinding the `| Nil` monad :p
bijan_ is now known as bijan_awaaaay
<FromGitter>
<hfabre> I think i should rethink my code so i can avoid nilable instance variable
bijan_awaaaay has quit [Quit: System has gone to sleep. ZZZzzz…]
<FromGitter>
<aisrael> ☝️ In general, that’s a best practice (in whatever language)
faustinoaq has joined #crystal-lang
rohitpaulk has joined #crystal-lang
A124 has quit [Disconnected by services]
A124 has joined #crystal-lang
TakeYourFreedom has quit [Remote host closed the connection]
TakeYourFreedom has joined #crystal-lang
maattdd has joined #crystal-lang
rohitpaulk has quit [Ping timeout: 248 seconds]
<FromGitter>
<hfabre> > **<hightower2>** Yes but since this is a property (instance variable) something could change it outside of your code ⏎ Just to be sure i understand the why, this protection is here to protect against race condition or is there any other case where it could happen ?
<FromGitter>
<hfabre> i mean i don't see other situation where it could happen
<FromGitter>
<hfabre> yes, using a local var works, i was just wondering why the compiler complain about this (appart to protect against race condition)
hmans has quit []
hmans has joined #crystal-lang
<FromGitter>
<aisrael> Still guessing: the compiler isn’t ‘smart’ or exhaustive enough to check *all* methods (incl. those in `include`d modules) for any other (reentrant) code that might modify `@channel`
<FromGitter>
<hfabre> hmm you should be right, thanks !
rohitpaulk has quit [Ping timeout: 276 seconds]
<hightower2>
right
<FromGitter>
<aisrael> Personally, that’s a good enough rationale for like a `final` keyword
mroth has quit []
<FromGitter>
<aisrael> (Still personally, and no offense to the core team, but I would’ve preferred if Crystal was immutable by default, with a `var` keyword to indicate otherwise)
<FromGitter>
<MrSorcus> > string.size.to_s(16)? ⏎ ⏎ Returned "4" not "\x04"
greengriminal has quit [Quit: This computer has gone to sleep]
Majost has quit []
Majost has joined #crystal-lang
<FromGitter>
<hfabre> @asterite got it thanks
faustinoaq has joined #crystal-lang
<FromGitter>
<asterite> Sorcus interpolate with string...
<FromGitter>
<asterite> "\\x#{...}"
<FromGitter>
<asterite> Not sure what's the use of that
alex`` has quit [Quit: WeeChat 2.0.1]
bijan_ has quit [Quit: System has gone to sleep. ZZZzzz…]
<crystal-gh>
[crystal] S-YOU opened pull request #5713: Add pointer support on Atomic (WIP) (master...atomic_pointer) https://git.io/vAmK8
<Papierkorb>
MrSorcus, "\x04" is only a representation, in memory it really is a byte of 0x04. Do you mean you need the size of that representation? What's your use case?
snsei has quit [Remote host closed the connection]
bijan_ has joined #crystal-lang
alex`` has joined #crystal-lang
<crystal-gh>
[crystal] S-YOU closed pull request #5713: Add pointer support on Atomic (WIP) (master...atomic_pointer) https://git.io/vAmK8
<FromGitter>
<MrSorcus> > **<Papierkorb>** @MrSorcus, "\x04" is only a representation, in memory it really is a byte of 0x04. Do you mean you need the size of that representation? What's your use case? ⏎ ⏎ `@socket.as(TCPSocket).write("\x05\x01\x00\x03\x10v6.ipv6-test.com\x00\x50".to_slice)` ⏎ This code used in socks proxy. `\x10` - size of address. [https://gitter.im/crystal-lang/crystal?at=5a81c1d1d74ee9f50db56bb9]
flaviodesousa has quit [Ping timeout: 276 seconds]
duane has joined #crystal-lang
rohitpaulk has quit [Ping timeout: 276 seconds]
bijan_ is now known as bijan_awaaaay
TakeYourFreedom has joined #crystal-lang
Take_Your_Freedo has quit [Read error: Connection reset by peer]
rohitpaulk has joined #crystal-lang
duane has quit [Ping timeout: 252 seconds]
<FromGitter>
<nbw> Hi guys, I'm using Kemal for this programming competition (basically classic snake game with multiple players) and I'm running into issues parsing JSON. I don't have a background with typed languages, which is probably why I'm running into issues. Here's what the a typical post json body looks like: ⏎ ⏎ https://gist.github.com/nbw/31f3873936a43fa297a7a682d9582a11 ⏎ ⏎ Kemal gives you access to the json
<oprypin>
looks like json mapping would work nicely for you
duane has joined #crystal-lang
greengriminal has quit [Quit: Leaving]
<oprypin>
and that would be `food_obj["data"].as(Array)` , anyway
<FromGitter>
<nbw> Yeah, I've considered that as well. What does it look like if you have Arrays of complex hashes? I couldn't find an example. ⏎ ⏎ What would the mapping of: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5a81e85118f388e62694ae09]
<oprypin>
nbw, you wrote .as(Hash) without any problems, then why do you hesitate about writing .as(Array) ?
<FromGitter>
<nbw> I think it asks me for a more specific array type.
<FromGitter>
<nbw> as it's not one of the kemal JSONTypes. I'd like to work around Kemal's parser, which you linked to. Reading up on that now
<oprypin>
thankfully you were able to open the link cuz the site seems down?
<FromGitter>
<nbw> oh, yeah. looks like it. well.. not closing that tab then :D
<oprypin>
nbw, yeah i dunno, you can try to go for JSON::Any with the linked workaround
<oprypin>
but note that this will all be gone in the next release of Crystal
<FromGitter>
<nbw> I mean i run into a similar problem as this guy did where i get an empty string.
<oprypin>
only JSON.mapping was good enough not to change
<FromGitter>
<nbw> but just trying that gets_to_end again.
<FromGitter>
<nbw> which is exactly what he does in kemal's json parser.
<oprypin>
nbw, are you trying the exact line that I linked?
<oprypin>
also note that you cant use any of that if you've already used .json
<oprypin>
cuz it has alreasdy been gotten to end, ya know?
<dragonkh>
the open ssl AES cipher support in Crystal is enforcing a key length of 16 - if I put less than 16 it complains - if I put more than 16 it only takes the first 16 - is there a way to allow the user to put a password of min 8 - and max 20
<dragonkh>
I'm finding openssl is a bit urrrrgh at the moment
<oprypin>
dragonkh, you realize that encrypting passwords is pointless?
<dragonkh>
I'm not trying to encrypt the password - just the data
<oprypin>
ok
<dragonkh>
but I want the user to not be restricted by having to put exactly 16 chars as the password
<oprypin>
hash the password, i dunno
<oprypin>
that sounds a bit silly but if you insist on that cipher, why not
<dragonkh>
its the same will all the ciphers I've tried
<dragonkh>
they have a key length hardcoded
<oprypin>
dragonkh, so i still say hash the key
<dragonkh>
the ruby version allows any size password - and it just uses openssl also
<oprypin>
well if ruby is so nice, go check its source code
<dragonkh>
I want the data to be decryptable in crystal and javascript - so I don't want to do anything specific to the crystal one to make it different
<dragonkh>
the javascript one also allows any size password
<dragonkh>
maybe I need to write my own one
<oprypin>
dragonkh, can i see proof regarding ruby?
dragonkh has quit [Quit: My iMac has gone to sleep. ZZZzzz…]
moei has quit [Quit: Leaving...]
duane has joined #crystal-lang
<FromGitter>
<bew> Yeah that's what Manas is working on since... quite a long time for crystal, but currently didn't publicly release anything about it :/
maattdd has joined #crystal-lang
<FromGitter>
<Wachiwi> Short question I am too inexperienced to solve: ⏎ ⏎ I have the following source code where I want to create a Fiber without the `spawn`-macro. But when I try to run it the result is the error below. Could anybody tell me what I am missing there? ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5a82242a4a6b0dd32bc20c1a]
<Papierkorb>
Why are you trying to get around the spawn macro?
<FromGitter>
<bew> note: `f1.yield` doesn't mean anything from where you call it
<FromGitter>
<Wachiwi> For university I need to explain concurrency in crystal and I wanted to unterstand it by fooling around and understanding it
<FromGitter>
<Wachiwi> @bew thx, good to know
<FromGitter>
<bew> it basically mean that from outside the fiber, you're stoping it, but it wasn't even running
<FromGitter>
<Wachiwi> @bew so `f1.run` would be the proper call?
<Papierkorb>
It 1) creates the fiber (like you did) and then 2) enqueues it into the scheduler, in other words, schedules it to be run at a later time
<FromGitter>
<bew> @Wachiwi `just `sleep` or `Fiber.yield` or `Fiber.current.yield` in the main fiber, and the execution will pass to the execution to another fiber
<Papierkorb>
Fibers are a bit brittle so you shouldn't (in the regular case) directly interface with them (For production code use channels instead)
<FromGitter>
<Wachiwi> thx for the explanations
<Papierkorb>
Depends on what you're actually explaining. The architecture? The scheduler? Channels?
<RX14>
ok, time to apply for the rust gsoc instead lol
<FromGitter>
<Wachiwi> @Papierkorb In general I only need to explain Fibers and Channels and how the help to solve race conditions etc.
<FromGitter>
<Wachiwi> So I am trying to break them down and understand them
<Papierkorb>
Wachiwi, Crystal employs an extremely similar approach to Go, so if you haven't already, you may have an easier time finding info on Go which basically applies 1:1 to Crystal
<FromGitter>
<Wachiwi> thanks for the hint, I will take a look
<Papierkorb>
In short, concurrency (and by extension parallelism) is a hard problem. The hard thing is not starting threads, but synchronization. Traditional means are e.g. Mutexes/Semaphores. As they're hard to get right, other means emerged. Some languages/frameworks used the Actor pattern for that. Other frameworks (e.g. Qt with C++) allowed you to run a single function (later, Lambdas) in a thread parallel to your program, without you having to
<Papierkorb>
mess with threads directly - Issue is, this isn't "interactive". You start them and wait for their result. Go (and Crystal) went beyond this by adding channels to add interactivity back
<FromGitter>
<CodeTrooperMC> One returns and one doesn't? Just a guess.
<FromGitter>
<CodeTrooperMC> @xfbs
<Papierkorb>
That's my TL;DR on the history of concurrent computing :P
<RX14>
@CodeTrooperMC incorrect
<Papierkorb>
xfbs, def is a Crystal method (only visible to Crystal), a `fun` is a C function (visible to Crystal and from the outside). fun's also use the C calling convention, Crystal methods don't guarantee you that
<RX14>
^
<FromGitter>
<xfbs> Ohhh gotcha
<RX14>
basically don't use `fun` outside of `lib`
<FromGitter>
<CodeTrooperMC> Okay. I was guessing though. Is Crystal out for Windows yet?
<RX14>
`fun` with body is only useful in a few small places in the stdlib
<RX14>
@CodeTrooperMC no
<FromGitter>
<xfbs> What are the roadblocks to crystal on windows?
<FromGitter>
<bew> also, @xfbs `def` is a Crystal method, you can pass any kind of types in arguments, and return any kind of types too, and `fun` can only use basic primitive types, line Int32, Char*, Pointer, ..
<RX14>
@xfbs manpower
<FromGitter>
<xfbs> Can I use crystal to write a library that is usable by C? Not that I need to, but like, hey