Version française
Home     About     Download     Resources     Contact us    
Browse thread
define incompatible type
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Guillaume Yziquel <guillaume.yziquel@c...>
Subject: Re: [Caml-list] define incompatible type
David Allsopp a écrit :
> David Rajchenbach-Teller wrote:
>>      Hi Grégoire,
>>  It's not directly possible in OCaml, but there are at least three methods
> for doing what you
>> want.
>>
>> The first one is to wrap your integers behind a constructor, e.g.
> 
> <snip>
> 
> You can also use (post OCaml 3.11.0) private types if you want to be able to
> use the ID values as integers but only explicitly.

Private types is indeed the way to go.

For instance, imagine that you have value wrapping C pointers. You may 
want to declare something like:

> type pointer
> type type_1 = private pointer
> type type_2 = private pointer

When you write your code, you essentially use only types type_1 and 
type_2. The type inference system won't look into the "private" part of 
the type, so the type checker will cough on something like

> function (x : type_1) (y : type_2) -> x = y

But, you can subtype (i.e. use :> to tell the typechecker to use the 
private declaration). And this will be valid OCaml:

> function (x : type_1) (y : type_2) -> (x :> pointer) = (y :> pointer)

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/