Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] References to 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 <garrigue@k...>
Subject: Re: [Caml-list] References to polymorphic variants
From: "Eric C. Cooper" <ecc@cmu.edu>

> Please ignore my last post.  I thought that since
> 
> > $ ocamlc -i -c foo.ml
> > val x : _[> `Apple | `Cheese | `Pair of int * string ] option ref
> 
> didn't complain, the compiler had accepted it, but it does complain when I
> omit the -i. (Is this a bug?)

It's not a bug: -i does the type checking, but does not actually
compile.
It's useful, because you can use this information to write your .mli.
In the above case, you would have to write in foo.mli:
  val x : [ `Apple | `Cheese | `Pair of int * string ] option ref
This makes sure your reference is no longer polymorphic, and makes the
compiler happy.

To go back to the original question, a polymorphic variant is just a
more refined way to the described the possible values of a sum type.
This is not a universal type, like variants in Visual Basic.
As the above example shows, you can still use polymorphic variants to
have the type checker compute the contents of a reference inside a
compilation unit, but this will not work across compilation units.
If you need a universal type, you can use [exn], the type of
exceptions, as exceptions defined in different modules can be
distinguished (whereas polymorphic variant tags cannot).

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners