Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] opaque polymorphism
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] Re: Phantom types (very long)
> This "phantom types" design pattern is one I have never seen before.  It 
> doesn't seem to be used in the standard library anywhere I can see.  It 
> looks like it might be useful in presenting a safer network programming 
> interface than the low-level wrappers around BSD sockets (which I've 
> never liked).

Thay are used in both Bigarray and Labltk (the 'a widget type). In
fact they were already used in Labltk before the word phantom started
to be used for them.

> Are there any other mind-blowingly elegant design patterns lurking in 
> the corners of the Caml type inference engine that I should know about?

I don't know if this is mind blowing, but thanks to variance
annotations on abstract types, you can also have subtyping on
"phantom" types. This is used in lablgtk for instance:

type (-'a) gtkobj
type widget = [`widget]
type container = [`widget|`container]
type box = [`widget|`container|`box]

now you can cast safely between classes:
    (mybox : box gtkobj :> container gtkobj)

Note that the parameter must be contravariant if you use polymorphic
variant types as indexes, or covariant if you rather use object types.

This also works ok with polymorphism, encoding inheritance (even
multiple) in a direct way.
val add : [> `container] gtkobj -> [> `widget] gtkobj -> unit

Cheers,

        Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr