Type inference error for array types

I am trying to create an implementation of a multiway tree.
While using Belt.Array or using Js.Array2, the type inference tells me
I have incompatible types as shown here and here

I still cannot figure out why the type is incompatible. What I do know is changing line 17 to

| true => Node(e1)

makes the types compatible. Can someone explain what am I doing wrong here?

The fact that arrays are used in the type definition is unrelated. The issue comes from the fact that your match function’s type is (e<'a>, e<'b>) => bool, so the typechecker infers that root and target both contain e<'a> types. Then when you try to pass a value of type t<intObj> instead of t<e<'a>, it creates the error.

Records are not structurally typed, so you can’t have a function that takes “any record with an id field” as an argument.

There are several ways you can solve this. One simple solution is to just use a different match function for the intObj type. Example.

1 Like

Understood. I would like the match function to work on 2 levels:
one for the e<'i> type and another for the 'i type.
Do I need a functor?

Any function can either work on e<'i> or 'i, but not both. Fundamentally, you need two functions, one for each type.

You don’t necessarily need to use a functor. If you can’t statically define the match_intobj function (like in my last example), then you can instead provide it as an argument for your deleteItem function. See this example.

A functor would be overkill for this scenario IMO, but here’s an example of how you would use it. It shares the same concept as the example above, but you parameterize the match function on the module level.

1 Like