Version française
Home     About     Download     Resources     Contact us    
Browse thread
"opening" record types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Philippe Wang <lists@p...>
Subject: Re: [Caml-list] "opening" record types
Yet another suggestion :

Instead of :
#  module M = struct type t = { a : int ; b : int } end ;;
module M : sig type t = { a : int; b : int; } end

Write :
#  module M = struct type t = { a : int ; b : int } module N = struct 
type t = M.t = { a : int ; b : int } end end ;;
module M :
  sig
    type t = { a : int; b : int; }
    module N : sig type t = M.t = { a : int; b : int; } end
  end

Then :

# include M.N;;
type t = M.t = { a : int; b : int; }

So that you can do :
# { a = 42 ; b = 23 } ;;
- : t = {a = 42; b = 23}

That way, when you change or add definitions, it's easy to spread 
changes, and you export only what you want to export...
:-)

Cheers,

--
Philippe Wang
  mail[at]philippewang.info


Yitzhak Mandelbaum wrote:
> Hi,
>
> I'm generally averse to opening modules, as I find it can make it hard 
> to figure out where definitions are coming from. Instead, I prefer to 
> bind only those pieces of the module that I need, and to do so 
> explicitly.  However, there is one instance in which I find myself 
> with little choice: record types.  In order to bind the 
> field-constructors for a record, you need to copy the entire type from 
> the module in which its declared. For example,
>
> module Foo = 
> struct
>   type t = {a: int; b:int}
> end
>
> without copying the type:
>
> let y = {Foo.a=3; Foo.b=4}
>
> with copying: 
>
> type t = Foo.t =  {a: int; b:int}
>
> let x = {a=3; b=4}
>
> While this is a nuisance to begin with, its even worse if the record 
> type is subject to change -- each time it changes, you need to update 
> every place where you copied the definition.
>
> Is there any way around this?  That is, is there any way to use the 
> field names of a record defined in another module without opening the 
> whole module or copying the whole definition of the record.
>
> Cheers,
> Yitzhak
>
> --------------------------------------------------
> Yitzhak Mandelbaum
> AT&T Labs - Research
>
> http://www.research.att.com/~yitzhak 
> <http://www.research.att.com/%7Eyitzhak>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>