<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> 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>
<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> `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>
<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