[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ 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: > > I'm getting confused while trying to implement some subtyping hierachy > with polymorphic variants. > say I have the hierachy: > > symbol -> image -> xpmimage > > and types like that: > > type 'a sym;; 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 > make_image: params -> [`Symbol | `Image] sym;; > make_xpm; params -> [`Symbol | `Image | `Xpm ] sym;; you can define type names, this gives shorter definitions for deep hierarchies: type symbol = [`Symbol] type image = [symbol | `Image] type xpm = [image | `Xpm] make_xpm : params -> xpm sym > so that some functions work only on some symbols: > > val get_image_width: [< `Image | `XpmImage] sym -> int;; > > I think I mix up the [< ..] and [> ... ] type constructs, allthough I > thought I understood it :-) that should be: val get_image_width : [> xpm] sym -> int -- Olivier