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