Version française
Home     About     Download     Resources     Contact us    
Browse thread
Smells like duck-typing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Daniel_Bünzli <daniel.buenzli@e...>
Subject: Re: [Caml-list] Smells like duck-typing
Use a record with a phantom type (the type must be abstract for this  
to work). The record has the common properties as fields and a data  
field which is either a list of tagged values (if many kinds of  
stories share the same fields) or a variant for the data specific to  
a kind of story. If there are operations that are allowed only on  
some kind of stories constrain the phantom type in the signature to  
enforce this statically.

Best,

Daniel

module Story : sig

  type 'a t constraint 'a = [< `Full | `Blurb | `Fresh ]

(* Story creators *)
  val full : ... -> `Full t
  val blurb : ... -> `Blurb t
  val fresh : ... -> `Fresh t

  val print_meta_data : [< `Full | `Blurb ] t -> unit

end = struct

   type 'a t = {
       id : int;
       title : string;
       intro : string;
       data : ...
    }
   ...
end