Playground: Recursive tree with polymorphic variants blows up the stack
Closed this issue · 3 comments
fhammerschmidt commented
The following example yields a stack overflow error on the playground
The same with normal variants does not
Melange Playground can do the polymorphic one without problems:
I also tested the polyvariants example locally with ReScript 11, where it compiles fine.
fhammerschmidt commented
Ok, so the polymorphic one also compiles when it's typed out and annotated!
fhammerschmidt commented
To summarize the issue:
-
This yields a stack overflow
let rec sum = x => switch x { | #Leaf => 0 | #Node(value, left, right) => value + left->sum + right->sum } let myTree = #Node( 1, #Node(2, #Node(4, #Leaf, #Leaf), #Node(6, #Leaf, #Leaf)), #Node(3, #Node(5, #Leaf, #Leaf), #Node(7, #Leaf, #Leaf)), ) let () = myTree->sum->Js.log
-
This one compiles
type rec tree<'value> = [ | #Leaf | #Node('value, tree<'value>, tree<'value>) ] let rec sum = (x: tree<'value>) => switch x { | #Leaf => 0 | #Node(value, left, right) => value + left->sum + right->sum } let myTree = #Node( 1, #Node(2, #Node(4, #Leaf, #Leaf), #Node(6, #Leaf, #Leaf)), #Node(3, #Node(5, #Leaf, #Leaf), #Node(7, #Leaf, #Leaf)), ) let () = myTree->sum->Js.log
fhammerschmidt commented
Fixed now.