Version française
Home     About     Download     Resources     Contact us    
Browse thread
Records with default values
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin_jambon@e...>
Subject: Re: [Caml-list] Records with default values
On Wed, 23 Mar 2005, Jeffrey Cook wrote:

> What I would like is the following:
>
> type bob = {
>     foo : string ;
>     bar : int := 0 ;
> }
>
> let x = { foo = "hello" }

I would go for a "create_bob" function with labelled arguments, some of
them being optional:

let rec create_bob ~foo ?(bar = 0) () =
  { foo = foo;
    bar = bar }

Implementing a syntax extension that defines "create_bob" automatically is
typically something that can be done with Camlp4 :-)

It supports recursive type definitions such as:

  type t = { x : t option = Some { x = None } }

or even (since create_t is recursive):

  type t = { x : t option = Some (create_t ~x:None ()) }

which would be expanded into:

  type t = { x : t option }
  let rec create_t ?(x = Some (create_t ~x:None ())) () = { x = x }


Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr