Home     About     Download     Resources     Contact us

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

Browse thread
# and polymorphic variants
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: Jacques Garrigue Subject: Re: # and polymorphic variants
```From: Juergen Pfitzenmaier <pfitzen@informatik.uni-tuebingen.de>

> Dear ocaml users,
> consider the following example:
>
> type t1 = [ `A of int ];;
> type t2 = [ `B of string ];;
> type t = [ `A of int | `B of string ];;
>
> let f1 (`A (x : int)) =
> 	print_int x
> and f2 (`B (x : string)) =
> 	print_string x
> and f (x : t) =
> 	match x with
> 	| #t1 -> f1 x      (* this is not allowed !! *)
> 	| #t2 -> f2 x;;
...
> But I would like to see something clean like giving a name to the # pattern
>     ...
> 	match x with
> 	| #t1 y -> f1 y    (* (y : t1) would have the same value as x *)

You were pretty close to the solution !
You just have to write
match x with
| #t1 as y -> f1 y
| #t2 as y -> f2 y

logical, no?

Jacques

```