From: Nicolas barnier <barnier@recherche.enac.fr>
> Marcin 'Qrczak' Kowalczyk wrote:
> >
> > What are variance annotations in OCaml?
>
> Perniciously stolen on the OCaml CVS server (file "Changes"), but I lack
> the new doc ;-):
>
> Objective Caml 3.01:
> --------------------
>
> New language features:
> - Variables are allowed in "or" patterns, e.g.
> match l with [t] | [_;t] -> ... t ...
> - "include <structure expression>" to re-export all components of a
> structure inside another structure.
> - Variance annotation on parameters of type declarations, e.g.
> type (+'a,-'b,'c) t (covariant in 'a, contravariant in 'b, invariant in 'c)
Yes, this is the syntax.
Practically, this means that you can now mix object and variant
subtyping with both datatypes and abstract types.
For instance, suppose that you have two types c and d, d subtype of c.
In ocaml 3.00, to coerce a list of d's to a list of c's, you would
have to write
List.map (fun x -> (x : d :> c)) l
In ocaml 3.01, you can do it directly
(l : d list :> c list)
because list is a covariant type, which you can see from its definition.
Not only that, but thanks to explicit variance annotations, even
abstract datatypes, like maps from the Map module, can be traversed by
subtyping.
My remark on a lesser need for "hacked" coercions is a side-effect of
that.
Jacques Garrigue