The following code causes a compiler error when calling node.left.to_list() and node.right.to_list() even though the function signature specifies the type of node.left and node.right as BinarySearchTree:
BinarySearchTree := [Nil, Node({ value : U64, left : BinarySearchTree, right : BinarySearchTree })].{
from_list : List(U64) -> BinarySearchTree
from_list = |data| {
data.fold(Nil, insert)
}
to_list : BinarySearchTree -> List(U64)
to_list = |tree| {
match tree {
Nil => []
Node(node) => {
node.left.to_list().append(node.value).concat(node.right.to_list())
}
}
}
insert : BinarySearchTree, U64 -> BinarySearchTree
insert = |tree, value| {
match tree {
Nil => Node({ value, left: Nil, right: Nil })
Node(node) => {
if value <= node.value {
Node({ ..node, left: node.left.insert(value) })
} else {
Node({ ..node, right: node.right.insert(value) })
}
}
}
}
}
main! = |_args| {
tree = BinarySearchTree.from_list([1,3,2,5,5,3,6,3,2,1])
echo!(tree -> Str.inspect)
list = tree.to_list()
echo!(list -> Str.inspect)
Ok({})
}
The error is:
-- MISSING METHOD --------------------------------
This is trying to dispatch a method named append on an unresolved type variable, but unresolved type variables have no methods.
┌─ /var/folders/74/7_g_vgn57nzgjh9mw5smp6rc0000gn/T/roc/debug-05d70690/MFa61CKCZdrtQX8lbdcS0Zf1zxu1gkzO/main.roc:18:5
│
18 │ node.left.to_list().append(node.value).concat(node.right.to_list())
│ ^^^^^^^^^^^^^^^^^^^
Hint: You can replace this static dispatch call with an ordinary function call, or force the type variable to become more concrete—for example, by adding a type annotation that narrows its type to something that actually has methods.
-- MISSING METHOD --------------------------------
This is trying to dispatch a method named concat on an unresolved type variable, but unresolved type variables have no methods.
┌─ /var/folders/74/7_g_vgn57nzgjh9mw5smp6rc0000gn/T/roc/debug-05d70690/MFa61CKCZdrtQX8lbdcS0Zf1zxu1gkzO/main.roc:18:5
│
18 │ node.left.to_list().append(node.value).concat(node.right.to_list())
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Hint: You can replace this static dispatch call with an ordinary function call, or force the type variable to become more concrete—for example, by adding a type annotation that narrows its type to something that actually has methods.
Found 2 error(s) and 0 warning(s) for /var/folders/74/7_g_vgn57nzgjh9mw5smp6rc0000gn/T/roc/debug-05d70690/MFa61CKCZdrtQX8lbdcS0Zf1zxu1gkzO/main.roc.
Note that the program works fine if we replace node.left.to_list() with to_list(node.left), and node.right.to_list() with to_list(node.right).
The following code causes a compiler error when calling
node.left.to_list()andnode.right.to_list()even though the function signature specifies the type ofnode.leftandnode.rightasBinarySearchTree:The error is:
Note that the program works fine if we replace
node.left.to_list()withto_list(node.left), andnode.right.to_list()withto_list(node.right).