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.

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.