English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Recursive subtyping issue
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-03-01 (11:21)
From: Guillaume Yziquel <guillaume.yziquel@c...>
Subject: Re: [Caml-list] Recursive subtyping issue
Stéphane Glondu a écrit :
> Guillaume Yziquel a écrit :
>>> # type untyped;;
>>> type untyped
>>> # type 'a typed = private untyped;;
>>> type 'a typed = private untyped
>>> # type -'typing tau = private obj
>>>   and 'a t = 'a typed tau
>>>   and obj = private untyped tau;;
>>> type 'a tau = private obj
>>> and 'a t = 'a typed tau
>>> and obj = private untyped tau
>>> # let f : 'a t -> obj = fun x -> (x : 'a t :> obj);;
>>> val f : 'a t -> obj = <fun>
>>> # let g : obj -> 'a t = fun x -> (x : obj :> 'a t);;
>>> val g : obj -> 'a t = <fun>
>>> # 
> Why don't you just declare 'a t to be synonym for obj in the
> implementation of your module, declare them as abstract in its
> interface, and export the specially typed identities f and g?

Because subtyping seems more efficient than applying a noop function. 
And this code might run really often, so I do not like very much the 
idea of having noop functions running really often.

Moreover, having conversion functions is not really handy, from a 
syntactic point of view: It's quite convenient to write something like

let f : string -> obj :> string -> float t = blah blah blah...

than doing the explicit, runtime, casting in the definition of f. 
Haven't tried the line above, so do not quote me on this, but using 
covariance and contravariance can make your code cleaner, with less 

What happened is that I was (am currently) doing only the 'a t :> obj 
subtyping in OCaml-R, and I also have a R.cast function to cast from obj 
to 'a t.

I thought this solution was cleaner than having two conversion functions.

I then tried to go the whole way, and get rid of conversion functions 

      Guillaume Yziquel