Version française
Home     About     Download     Resources     Contact us    
Browse thread
Conditionals based on phantom types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Lukasz Stafiniak <lukstafi@g...>
Subject: Re: [Caml-list] Conditionals based on phantom types
You can index values by types, i.e. store the units with the floats in
the values. Otherwise, types are erased and not accessible.

module Units : sig
   type 'a t
   val to_feet : float -> [`Feet ] t
   val to_meters : float -> [`Meters] t
   val add : 'a t -> 'a t -> 'a t
   val print : 'a t -> unit
end = struct
   type 'a t = 'a * float
   let to_feet x= `Feet, x
   let to_meters x= `Meters, x
   let add (_,x) (_,y) = x +. y
   let print (u,x) = Printf.printf "%f %s" x (to_string u)
end;;


On Mon, Aug 2, 2010 at 9:07 AM, Joseph Young <ocaml@optimojoe.com> wrote:
> Hi,
>        Is there any way to write a conditional based on the type information
> of a value?  Specifically, if we use phantom types to write a module such as
>
> module Units : sig
[...]