Version française
Home     About     Download     Resources     Contact us    
Browse thread
polymorphic variant oddity
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <mottl@m...>
Subject: Re: polymorphic variant oddity
On Sat, 07 Oct 2000, Julian Assange wrote:
> let `F x = x;;   
> Unbound value x
> # let `F x = 1;;
> This expression has type int but is here used with type [< `F of 'a | ..]
> # let `F x = `F 1;;
> val x : int = 1
> # `F 4;;
> - : [> `F of int] = `F 4
> 
> What exactly is the meaning this?

This is the same as saying, e.g.:

  let Some x = Some 3;;

"x" will be bound to "3".

"let", too, can be used for pattern matching. Of course, one can only match
one pattern at a time with it, which rules it out for sum types that have
several instances (as in the above example -> "Some/None"): a match error
may occur.

Normally, this kind of pattern matching is only used for patterns that
never fail ("irrefutable patterns"), e.g. tuples:

  let x, y = 1, 2 in ...

Best regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl