jhass changed the topic of #crystal-lang to: The Crystal programming language | http://crystal-lang.org | Crystal 0.28.0 | 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
hightower2 has quit [Ping timeout: 252 seconds]
<FromGitter> <jgaskins> Is `def foo(&block : T ->) forall T` not a thing? I can't seem to get it to work.
<FromGitter> <asterite> @Blacksmoke16 you can't dup a pull parser, what you read can't be un-read. Maybe look at how a union is parsed: https://github.com/crystal-lang/crystal/blob/cd503777173f98fa9ff8306e750769f99f83c3e5/src/json/from_json.cr#L218
<FromGitter> <Blacksmoke16> but shouldnt making a dup of it allow it to work?
<FromGitter> <Blacksmoke16> as the dupped one could get the value of the "type" property, while the real one would be used in actually creating a new obj?
<FromGitter> <Blacksmoke16> @asterite ^
<FromGitter> <Blacksmoke16> as if i print `pull.kind` in `get_class` its `:begin_object`
<FromGitter> <Blacksmoke16> which im assuming is the beginning of the first object
<FromGitter> <Blacksmoke16> oh `you can't dup a pull parser`, well nvm i guess then
<FromGitter> <Blacksmoke16> was thinking the dupped one would allow you to read independently from the original
<FromGitter> <jgaskins> yeah, when you dup the parser, you've still already consumed the data from the underlying stream
<FromGitter> <Blacksmoke16> i.e. even if you dup it, they still will alter the same stream?
<FromGitter> <jgaskins> yep
<FromGitter> <Blacksmoke16> rip, thats a bummer
<FromGitter> <jgaskins> `dup` is a shallow copy
<FromGitter> <Blacksmoke16> ohh right
<FromGitter> <Blacksmoke16> separate instances of a pull praser, but each still reference same stream since ivars arent copied
<FromGitter> <jgaskins> bingo
<FromGitter> <jgaskins> it's one of the gotchas I run across in structs all the time, too. :-)
<FromGitter> <Blacksmoke16> darn, well there goes my idea
<FromGitter> <jgaskins> "it's fine, it copies it to the new stack frame" welllllll… 😂
<FromGitter> <Blacksmoke16> challenge is you know to what the type of the obj is before you can deserialize it, and since you cant rewind the pull parser makes it not work :/
<FromGitter> <Blacksmoke16> oh well
<FromGitter> <Blacksmoke16> got an initial version of https://github.com/Blacksmoke16/crylog/tree/master/docs going
<FromGitter> <Blacksmoke16> feedback welcome :)
<FromGitter> <Blacksmoke16> Basically just need to do more testing with it and add more handlers and such
<FromGitter> <jgaskins> @Blacksmoke16 I had a go at it and this is what I got. I had to tweak the JSON to separate the properties from the metadata (which is *super* common in things like this for static languages), but I managed to get it working. https://play.crystal-lang.org/#/r/6yv7
<FromGitter> <Blacksmoke16> hmm clever solutoin
<FromGitter> <jgaskins> Unfortunately, that code as written does require the `type` key to come before the `props` key in the JSON payload, so key order does matter. For production-quality code you might want to extract the actual construction of the object outside of the `pull.read_object` block.
<FromGitter> <Blacksmoke16> i had a version working earlier, but was outside the converter. like did JSON.prase, grab the type, the do like `SomeType.from_json obj.to_json` :/
<FromGitter> <Blacksmoke16> gotcha
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/6yv8 seems to work when props comes first
<FromGitter> <Blacksmoke16> :shrug:
<FromGitter> <jgaskins> Try doing that with the first object :-)
<FromGitter> <Blacksmoke16> ah, oh well
<FromGitter> <jgaskins> It worked because in the second iteration the `type` happened to be set to the same type as what that object needed since the objects in the array are the same type. If you had a heterogeneous array, it'd break.
<FromGitter> <jgaskins> lol Yeah, parsers in static languages can be rough. Works much better with binary protocols where you are usually guaranteed to get metadata before regular data.
rohitpaulk has joined #crystal-lang
rohitpaulk has quit [Remote host closed the connection]
<FromGitter> <watzon> Is there a reason why we don't have a `block_given?` macro?
<FromGitter> <ezrast_gitlab> @watzon Presence/absence of a block isn't dynamic like ruby, it's part of the method signature. The method author gets to decide whether there's a block just like they get to decide what the arguments are.
jetpack_joe has quit [Ping timeout: 252 seconds]
confact has quit [Read error: Connection reset by peer]
<FromGitter> <watzon> @ezrast_gitlab wait, so it should then be possible to check if a block has been given just like you would with any other parameter right? I thought you couldn't, but then maybe that changed since I last tried like 2 years ago.
<FromGitter> <watzon> Or maybe I'm just stupid. Who knows?
<FromGitter> <watzon> ,
<FromGitter> <yxhuvud> Usually, you get cleaner code by splitting it into two methods anyhow, one that take the block and one that doesn't.
<FromGitter> <watzon> You also get a lot of duplicated code though
<FromGitter> <watzon> I'm not a fan of duplicating code
<FromGitter> <yxhuvud> often, you can define one in terms of the other.
<FromGitter> <ezrast_gitlab> Isn't that the *only* way to do it? Otherwise you get `'foo' is expected to be invoked with a block, but no block was given`
jetpack_joe has joined #crystal-lang
confact has joined #crystal-lang
<FromGitter> <watzon> See that's what I thought
<FromGitter> <watzon> That's why it would be nice to be able to check for a block
<FromGitter> <watzon> I just like to do things the DRY way and the way blocks are handled makes that difficult sometimes
<FromGitter> <watzon> Also, I hate having to use `not_nil`, but I seem to have to use it a lot
<FromGitter> <j8r> you can use at least `as T` instead of `not_nil!`
<FromGitter> <watzon> How? Do you have an example?
<FromGitter> <watzon> This is my code right now. I've tried without `not_nil!` and the compiler screams at me every time ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5ce7a5d4ad024978c61d42bf]
<FromGitter> <j8r> if you have a variable `var` which is `String | Nil`, you can use `var.as(String)` instead of `var.not_nil!`
<FromGitter> <watzon> Ahh well that's true, but just as ugly
<FromGitter> <j8r> no
<FromGitter> <j8r> because instead of having a dumb `Nil assertion failed`
<FromGitter> <yxhuvud> The way to get around that is to do `if last_response = @last_response` and then work on last_response
<FromGitter> <j8r> you got at least something like `Fail to cast T to U`
<FromGitter> <j8r> yes, the best way is to do like @yxhuvud
<FromGitter> <j8r> and have proper errors, even it's more verbose
<FromGitter> <watzon> I feel like I should be able to do this ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5ce7a653d22ba766a2eeb252]
<FromGitter> <watzon> But the compiler screams about `@last_response` being nillable
<FromGitter> <j8r> do as @yxhuvud said
<FromGitter> <j8r> `if last_response = @last_response`
<FromGitter> <watzon> `links` is also nillable though, so I'd have to do that for both I suppose
<FromGitter> <alex-lairan> Same for `links`
<FromGitter> <alex-lairan> Yep
<FromGitter> <yxhuvud> @watzon instance variables can be changed by some other fiber or thread, so the compiler can't know that the type won't change. It can, if you assign it to a local variable.
<FromGitter> <alex-lairan> `if links = @last_response.try { |r| r.links }`
<FromGitter> <watzon> @yxhuvud that makes sense
<FromGitter> <j8r> of `@last_response.try &.links["next"]?`
<FromGitter> <j8r> depends of what you need to do
<FromGitter> <watzon> @alex-lairan that's an awesome use if `try`, don't know why I didn't think of that
<FromGitter> <alex-lairan> 👍
<FromGitter> <watzon> Same with you @j8r
<FromGitter> <watzon> Thanks guys
ShalokShalom has joined #crystal-lang
Liothen has quit [Ping timeout: 250 seconds]
Liothen has joined #crystal-lang
ashirase has quit [Ping timeout: 258 seconds]
ashirase has joined #crystal-lang
<FromGitter> <bew> @asterite @Blacksmoke16 to avoid the issue that is explained by `you can't dup a pull parser`, would it be ok to raise in `dup` for that type? (OR use more trait like stuff, and force the inclusion of some `Std::Dupable` for types that can be dup-ed)
<FromGitter> <watzon> Does `method_missing` not work on modules?
<FromGitter> <watzon> Guess not. Looks like `method_missing` and `forward_missing_to` only work with things that have been instantiated. I guess since modules can't be instantiated that leaves them out. https://carc.in/#/r/6yx7
<FromGitter> <watzon> @asterite @bew would it be possible to make `method_missing` work in modules? If not currently do you think it would be possible to implement in Crystal in the future? Or is there a good reason why isn't and shouldn't be that way?
DTZUZU2 has quit [Read error: Connection reset by peer]
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
return0e has joined #crystal-lang
<FromGitter> <bew> I think you mean @bcardiff, not me
laaron has quit [Remote host closed the connection]
<FromGitter> <bcardiff> (On mobile)I don’t think so. If multiple modules compete there is no clear winner. Picking any would leave lots of undefined behavior since method missing usually defines a whole bunch of methods
laaron has joined #crystal-lang
<FromGitter> <Blacksmoke16> @bew i mean you *can* dup a pull parser, but just the fact that dup is a shallow copy means the underlying ivars dont get dupped as well
<FromGitter> <Blacksmoke16> i.e. the dupped copy wont be independent from the original
<FromGitter> <Blacksmoke16> are the docs on why that doesnt work
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
<FromGitter> <asterite> @watzon `method_missing` already works in modules, but in general `method_missing` is kind of a mess that I'd like to remove from the language
<FromGitter> <pynixwang> include multi `method_missing ` modules? pick the last one?
laaron has quit [Remote host closed the connection]
return0e_ has joined #crystal-lang
return0e_ has quit [Remote host closed the connection]
laaron has joined #crystal-lang
DTZUZU2 has joined #crystal-lang
<FromGitter> <asterite> the first (closer) one, just like in any regular method lookup
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
<FromGitter> <jojokoro> Do we have any implementation of simple low-memory db in Crystal
<FromGitter> <Blacksmoke16> sqlite?
<FromGitter> <jojokoro> e.g. a simple API built on a db.json file
<FromGitter> <Blacksmoke16> most ORMs support sqlite, would be a place to start
<FromGitter> <jojokoro> sqlite is good, but was thingking nosql
<FromGitter> <jojokoro> something like lowDB for JS, for example idk
<FromGitter> <Blacksmoke16> not that comes to mind but wouldnt be super hard to do
<FromGitter> <j8r> RocksDB?
<FromGitter> <Blacksmoke16> esp since you can do like `Obj.from_json` and `obj.to_json` so reading/writing would be pretty easy. the main challenge would be if there is a way to rewrite only a section of the file. although might be easier to just rewrite the whole file
<FromGitter> <Blacksmoke16> that works too
<FromGitter> <Blacksmoke16> redis could work too, just store json strings with model name as the key
<FromGitter> <Blacksmoke16> https://github.com/vladfaust/tarantool.cr looks like another option
gangstacat has joined #crystal-lang
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
sympatico has quit [Quit: ZNC - http://znc.in]
ShalokShalom has quit [Remote host closed the connection]
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
ShalokShalom has joined #crystal-lang
ShalokShalom has quit [Remote host closed the connection]
<FromGitter> <vladfaust> Crystal's got sponsor badge
<FromGitter> <vladfaust> Ah, no, it's not on GitHub yet
<FromGitter> <vladfaust> Anyway that's cool
laaron has quit [Quit: ZNC 1.7.1 - https://znc.in]
laaron has joined #crystal-lang
<pracabor> I can see it
sympatico has joined #crystal-lang
ShalokShalom has joined #crystal-lang
ShalokShalom has quit [Remote host closed the connection]
moei has joined #crystal-lang
rohitpaulk has joined #crystal-lang
rohitpaulk has quit [Remote host closed the connection]