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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Olivier Andrieu <oandrieu@n...>
Subject: Re: [Caml-list] phantom type thing question
On 3/1/07, micha <micha-1@fantasymail.de> wrote:
> Am Thu, 1 Mar 2007 14:47:47 +0100
> schrieb "Olivier Andrieu" <oandrieu@gmail.com>:
>
> > On 3/1/07, micha <micha-1@fantasymail.de> wrote:
> > >
> > if you want subtyping, you'll need to add a variance annotation to
> > your type parameter. Otherwise since your type sym is abstract, you'll
> > get no subtyping at all. In this case you probably want a
> > contravariant type parameter:
> >
> > type -'a sym
>
> o.k., but after reading section 6.8.1 of the manual I don't see why it
> needs to be -'a and not +'a
>
>
> > that should be:
> > val get_image_width : [> xpm] sym -> int

your parameter encodes some "capabilities": you want the function
get_image_width to accept values that have the tag `xpm and possibly
more tags, hence the [> ] in the type of the function.
In other words, you want the type xpm sym to be a subtype of
[xpm|`xpm8] sym (for instance), so that get_image_width accept boths.
Since [xpm|`xpm8] is a subtype of xpm sym (the other way around), that
means the parameter needs to be contravariant.