Parameterized Types in OCaml

I have been looking for some time and I cannot find a solution. This is probably a simple syntax issue that I cannot understand.

I have a type:

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

And I want to create a type variable 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 tried a different way of placing brackets around type parameters, I get almost the same error.

It works, however, with single parameter types, so I guess there is some kind of syntax that 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?

+3
source share
2 answers

You have to write

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

This parameter mytypeis a pair. You can even remove unnecessary parentheses:

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

, mytype , . let x = ("v", "m").

$ ocaml
        OCaml version 4.01.0

# type ('a, 'b) mytype = 'a * 'b;;
type ('a, 'b) mytype = 'a * 'b
# let x = ("v", "m");;
val x : string * string = ("v", "m")
# (x : (string, string) mytype);;
- : (string, string) mytype = ("v", "m")

, string * string (string, string) mytype .

+1

All Articles