Version française
Home     About     Download     Resources     Contact us    
Browse thread
New language feature in OCaml 3.11
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jérémie Dimino <jeremie@d...>
Subject: Re: [Caml-list] New language feature in OCaml 3.11
On Fri, Dec 05, 2008 at 12:57:42PM +0100, Paolo Donadeo wrote:
> > - Subtyping is now allowed between a private abbreviation and its definition,
> >  and between a polymorphic method and its monomorphic instance.
>
> Is there anybody who wants to elaborate this with an example,
> especially the second statement, regarding polymorphic methods?

Here is an example for private abbreviation:

,----
| module M : sig
|   type t = private int
|   val x : t
| end = struct
|   type t = int
|   let x = 1
| end
`----

[M.x] can be seen as a value of type [int] by using a coercion:

,----
| # M.x;;
| - : M.t = 1
| # (M.x :> int);;
| - : int = 1
`----

And one example for polymorphic methods:

,----
| class type foo = object
|   method f : int -> int
| end
|
| let x = object
|   method f : 'a. 'a -> 'a = fun x -> x
| end
`----

with ocaml < 3.11 [x] can not be coerced to type [foo]:

,----
| # (x :> foo);;
| Characters 1-2:
|   (x :> foo);;
|    ^
| This expression cannot be coerced to type foo = < f : int -> int >;
| it has type < f : 'a. 'a -> 'a > but is here used with type #foo
| Types for method f are incompatible
`----

but it can with ocaml 3.11:

,----
| # (x :> foo);;
| - : foo = <obj>
`----

Jérémie