Hashnut
Hashnut

Reputation: 377

Ways to define ocaml function

I tried to define function which type is

(test, test) -> test.

So I wrote down my code like this :

let test_func: (test, test) -> test = fun ...

But It gave me syntax error, so I changed it to

let test_func: (test, test) test = fun ...

And It doesn't give me syntax error message.

But I don't know why the first one gives me the syntax error..

(I also tried this form. let test_func (test, test): test Is this the better way to define function?)

Upvotes: 1

Views: 68

Answers (1)

glennsl
glennsl

Reputation: 29106

(test, test) isn't a valid type. If your intent is to describe a tuple type, the right syntax is test * test. I suspect you actually want to describe a function with two arguments, however, which would be test -> test -> test because OCaml is a curried language.

(test, test) test is syntactically valid because it describes a parameterized type that's given two arguments.

let test_func (test, test): test does not specify the entire function type, just the return type. (test, test) here describes a single tuple argument where the items are bound to test and test respectively. I'm guessing you're actually using different names though, otherwise this wouldn't compile.

You can specify the type of each argument and the return type separately like this:

let test_func (a: test) (b: test): test = ...

Upvotes: 1

Related Questions