ChanServ changed the topic of #crystal-lang to: The Crystal programming language | https://crystal-lang.org | Fund Crystal's development: https://crystal-lang.org/sponsors | GH: https://github.com/crystal-lang/crystal | Docs: https://crystal-lang.org/docs | Gitter: https://gitter.im/crystal-lang/crystal
notzmv has quit [Ping timeout: 264 seconds]
notzmv has joined #crystal-lang
Sankalp has quit [Ping timeout: 256 seconds]
Sankalp has joined #crystal-lang
HumanG33k has quit [*.net *.split]
dostoyevsky2 has quit [*.net *.split]
DeBot has quit [*.net *.split]
Stephie has quit [*.net *.split]
dostoyevsky2 has joined #crystal-lang
HumanG33k has joined #crystal-lang
Stephie has joined #crystal-lang
fifr has quit [*.net *.split]
xybre has quit [*.net *.split]
fifr has joined #crystal-lang
xybre has joined #crystal-lang
hightower3 has joined #crystal-lang
hightower2 has quit [Ping timeout: 244 seconds]
bastienleonard has joined #crystal-lang
avane has quit [Ping timeout: 276 seconds]
avane has joined #crystal-lang
Sankalp has quit [Ping timeout: 240 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 260 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 272 seconds]
Sankalp has joined #crystal-lang
hightower2 has joined #crystal-lang
hightower3 has quit [Ping timeout: 276 seconds]
sagax has quit [Remote host closed the connection]
ur5us has joined #crystal-lang
sagax has joined #crystal-lang
hightower2 has quit [Remote host closed the connection]
<FromGitter> <cinnamonz:matrix.org> I'm learning about the type system of Crystal and got stuck at satisfying the compiler while porting something from a dynamic language. What'd be a good approach to "type" and process a recursive data structure like this: ⏎ ⏎ ```data = ["+", [ ["+", 3, 2], ["+", 3, 2, 1] ] ]``` [https://gitter.im/crystal-lang/crystal?at=62cc5b329a314a3ec46acf2e]
<FromGitter> <Blacksmoke16> oof
<FromGitter> <Blacksmoke16> probably something like https://github.com/crystal-lang/crystal/issues/5155
<FromGitter> <Blacksmoke16> similar to how `JSON::Any` is setup, where you have a struct that has a single ivar typed as a union of the possible actual values, and itself
<FromGitter> <Blacksmoke16> ```code paste, see link``` ⏎ ⏎ something like this [https://gitter.im/crystal-lang/crystal?at=62cc5c17568c2c30d389dd0a]
<riza> @cinnamonz can you post more of the code you're tinkering with?
<riza> it looks like a typical postfix/infix calculator exercise
<riza> if you're just using data as a method of input, you may not need to actually type it, it can just be represented in some other way that's not literal -- a string or json string, for example
<FromGitter> <cinnamonz:matrix.org> Trying to interpret a simple lisp, as of now I'm trying to satisfy the type checker so that I can do something like: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=62cc5e8f76cd751a2ffb6dce]
<FromGitter> <cinnamonz:matrix.org> With the struct example, I'm getting: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=62cc5efc1227f62be35bd787]
<FromGitter> <Blacksmoke16> yea each item in the array needs to be an instance of `Token`
<FromGitter> <Blacksmoke16> but
<FromGitter> <cinnamonz:matrix.org> Oh
<FromGitter> <Blacksmoke16> if you can be assured that based on your logic that the tokens related to a `"+"` token are numbers might be able to just do like
<FromGitter> <Blacksmoke16> ```code paste, see link``` ⏎ ⏎ or something along those lines [https://gitter.im/crystal-lang/crystal?at=62cc5f573bbb38488938ff1b]
ur5us has quit [Ping timeout: 272 seconds]
<FromGitter> <Blacksmoke16> got a small example of the error you get when running with what you have atm?
<riza> I think your type is much more concrete than you're trying for. Each Operation is a String plus some number of Integer arguments
<FromGitter> <Blacksmoke16> hard part is its recursive
<FromGitter> <Blacksmoke16> but yea, prob dont need to explicitly type everything, can just use some `.as` in right places maybe
<FromGitter> <cinnamonz:matrix.org> Before your suggestions, I was at a point using type aliases like: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=62cc63379f29d42bed3a3f48]
<FromGitter> <cinnamonz:matrix.org> With this the errors are either compile time type mismatch for exp[0] or if I cast that, problems with exp[1] and exp[2]
<FromGitter> <cinnamonz:matrix.org> Now I will try with the struct you've suggested
<riza> is there an implicit operation where there is no string first parameter?
<riza> or is this a redundant set of `[]`? `[["+", 3, 2], ["+", 3, 2, 1]]`
<riza> I would expect that you'd need an operator at the beginning of that, something like `["+", ["+", 3, 2], ["+", 3, 2, 1]]` -- otherwise when each of the "inner" operations finishes you'll be left with [5, 6] which isn't an operation
<riza> perhaps just a copy pasta error
<FromGitter> <cinnamonz:matrix.org> Oh yeah that's certainly a copy pasta error, the outer `[]` is redundant
<riza> phew
<riza> @cinnamonz - I'm able to build an interpreter without changing the syntax of your `data` line -- no concrete types required, in other words.
notzmv has quit [Ping timeout: 276 seconds]
ur5us has joined #crystal-lang
ur5us has quit [Ping timeout: 268 seconds]
<FromGitter> <cinnamonz:matrix.org> @riza Sorry I was AFK. In total I wrote 3 test inputs. Have you tried with this line? ⏎ ⏎ `data = ["+", ["+", 3, 2], ["+", 3, 2, 1]]`
antoni has quit [Quit: WeeChat 2.8]
<riza> yes
<riza> `data = ["+", ["*", ["+", 3, 2], ["+", 3, 2, 1]]]` too
<FromGitter> <cinnamonz:matrix.org> Awesome, I'll try to get to the answer without concrete types then
<FromGitter> <Blacksmoke16> should be doable long as you dont need to store it in an ivar
<FromGitter> <Blacksmoke16> no real need to type method args for stuff like this
<FromGitter> <Blacksmoke16> some well placed `.as` and `is_a?` might just be enough
<FromGitter> <cinnamonz:matrix.org> well, at later stages it'd be great to implement concrete types, for that I'll look into the `JSON` example you've shared
<FromGitter> <Blacksmoke16> could break it down even further to something like `AddOperation.new(AddOperation.new(3, 2), AddOperation.new (3, 2, 1))`
<FromGitter> <Blacksmoke16> where they accept `Int32 | Operation`
<FromGitter> <Blacksmoke16> a splat of*
<FromGitter> <Blacksmoke16> :shrug:
<FromGitter> <Blacksmoke16> quite a few ways you could go about it
<riza> I used an `.as(Int32)` and an `.as(String)` -- one or the other of those might not have been necessary
Sankalp has quit [Ping timeout: 276 seconds]
<riza> it's a recursive parser, which isn't the only way to solve this, but it's the way that makes most sense in my brain
<riza> remembering back 15 years or so it's more performant to do a stack of some sort, I think
<riza> theres probably a bug in here which has something to do with the order of operations, too
Sankalp has joined #crystal-lang
<FromGitter> <cinnamonz:matrix.org> This was my solution: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=62cc7dab76cd751a2ffc4fa4]
<riza> nice, simple and straightforward
<FromGitter> <Blacksmoke16> 💪
<FromGitter> <cinnamonz:matrix.org> Now making this handle more operands and operators is straightforward, and for concrete types I'd need to break things down @Blacksmoke16.
<FromGitter> <cinnamonz:matrix.org> But this example completely ignores the parsing stage, normally we would know more about these tokens before we start interpreting.
<FromGitter> <cinnamonz:matrix.org> I'll do that stage first and then attempt concrete types.
<FromGitter> <cinnamonz:matrix.org> Thanks for all the help folks
notzmv has joined #crystal-lang
hightower2 has joined #crystal-lang
<FromGitter> <cinnamonz:matrix.org> ☝️ Edit (https://gitter.im/crystal-lang/crystal?at=62cc7dab76cd751a2ffc4fa4): This was my solution: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=62cc8c9d9f73251a2c14eac0]
hightower2 has quit [Remote host closed the connection]