What's the difference between these two type definitions in OCaml [duplicate]

Tag: ocaml Author: xushihuinuaa Date: 2014-01-24

This question already has an answer here:

type t1 = A of int * string and type t2 = A of (int * string), are they different or the same?

In this functional programming tutorial, slide 6, it says

In OCaml, variants take multiple arguments rather than taking tuples as arguments: A of int * string is di?erent than A of (int * string). But it’s not important unless you get bitten by it.

But I don't see any difference other than a pair of parenthesis.

These are two different types. This has been asked and answered many times on SO. Here are examples: click me

Best Answer

Try the following:

type t1 = A of int*int
type t2 = B of (int*int)
let x = (1,2) in A x (* does not work *)
let x = (1,2) in B x (* works *)

That is, B is a constructor expecting 1 argument (namely a tuple containing two integers), while A is a constructor accepting 2 arguments (supplied as a parenthesized, comma-separated thing).