Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] variant with tuple arg in pattern match?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-04-05 (17:39)
From: Alain Frisch <frisch@c...>
Subject: Re: [Caml-list] variant with tuple arg in pattern match?

On Wed, 4 Apr 2001, Chris Hecker wrote:

> I expected ii to be bound to the int * int tuple (1,2) in this pattern match:
> # type foo = Foo of int * int;;
> type foo = Foo of int * int

You define Foo to be a constructor with two arguments ...

(the star here is not the same as the one for tuple types)

> #   match Foo (1,2) with Foo ii -> ii ;;

and you use it as an unary constructor. Hence the error:

> Characters 23-29:
> The constructor Foo expects 2 argument(s),
> but is here applied to 1 argument(s)

The solution is to declare:

type foo = Foo of (int * int)

The drawback is that the internal representation of the value Foo (1,2)
uses two heap allocated blocks (one for the constructor, one for the
tuple). A flat representation, as is the case with your declaration, may
be more efficient, but then you have to explicitely reconstruct the tuple:

match Foo (1,2) with Foo (a,b) -> (a,b);;

The issue probably deserves a few words in the FAQ or in the manual.

Hope this helps.

Alain Frisch
To unsubscribe, mail  Archives: