Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parametrized types in OCaml

I have searched for some time and I can't find a solution. It's probably a simple syntax issue that I can't figure out.

I have a type:

# type ('a, 'b) mytype = 'a * 'b;;

And I want to create a variable of type string string sum.

# let (x:string string mytype) = ("v", "m");;
Error: The type constructor mytype expects 2 argument(s),
   but is here applied to 1 argument(s)

I've tried different way of putting parenthesis around type parameters, I get pretty much the same error.

It works, however, with single parameter types so I guess there's some syntax I don't know.

# type 'a mytype2 = string * 'a;;
# let (x:string mytype2) = ("hola", "hello");;
val x : string mytype2 = ("hola", "hello")

Can someone, please, tell me how to do this with two parameters?

like image 988
Vlad V Avatar asked Jan 24 '26 13:01

Vlad V


1 Answers

You should write

let (x: (string, string) mytype) = ("v", "m");;

That is mytype parameter is a couple. You can even remove unneeded parentheses:

let x: (string, string) mytype = "v", "m";;
like image 186
hivert Avatar answered Jan 26 '26 10:01

hivert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!