imalgrab
imalgrab

Reputation: 23

Constructor argument mismatch

I've created a type for lazy binary tree:

type 'a lBT = LEmpty | LNode of 'a * (unit -> 'a lBT) * (unit -> 'a lBT);;

and wanted to create an instance of it:

let exlBST = LNode(3, function() -> LEmpty, function() -> LEmpty);;

but I received this error:

Error: The constructor LNode expects 3 argument(s),
   but is applied here to 2 argument(s)

Can you tell me what's going on? Isn't unit considered as an argument?

Upvotes: 0

Views: 261

Answers (1)

glennsl
glennsl

Reputation: 29106

The argument to LNode is not parsed as you expect, a triple with two functions, but as a pair where the the second item is a function returning another pair. Use parentheses around at least the middle function, but preferably both for consistency, to make your intention explicit:

let exlBST = LNode (3, (function () -> LEmpty), (function () -> LEmpty));;

There are unfortunately a few of these weird parsing edge cases/ambiguities with OCaml's syntax. You'll learn to deal with it in time, but a good rule of thumb is: If in doubt, use parentheses (or begin/end when appropriate)

Upvotes: 2

Related Questions