caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Records with default values
@ 2005-03-23 19:41 Jeffrey Cook
  2005-03-23 20:25 ` [Caml-list] " Martin Jambon
  0 siblings, 1 reply; 2+ messages in thread
From: Jeffrey Cook @ 2005-03-23 19:41 UTC (permalink / raw)
  To: caml-list

Is there an existing (or even possible to do with) camlp4 extension to
support default values in records?  I am constantly finding myself
doing the following:

type bob = {
    foo : string ;    <--- field for which no default value makes sense
    bar : int ;         <--- field(s) that have default values
}

let bob_default = { foo = ""; bar = 0; }

with usage:

let x = { bob_default with foo = "hello" }

so that I do not have to fill in the numerous fields that really
should default to some value.

Without using this solution, my code is often cluttered with many
doldrum default value declarations for record fields (worsened by
multiple sites that define the initial record values), obscuring what
the important assignments were.  Additionally, I often find that the
complex fields (of record types, for example) are the ones that don't
lend themselves to a default value whereas the simple types do.  This
leads to the use of options (discussed below) just to save from even
uglier default value declarations for fields that never use a default.

Using a default structure like above also does not let me harness the
type checking mechanisms to detect places in my code where a field
(that really shouldn't have a default value) wasn't defined as is
normally the case when constructing a record from scratch - especially
when fields are later added to the record.

Using 'options' for no-default-value fields doesn't seem to be a very
good solution, as there then need to be runtime deconstructors of the
options (and runtime checks or uncaught exceptions) if a field has a
None value that should have been set.  All that aside, it still
wouldn't identify where the record was incompletely 'created' using
the default record.

What I would like is the following:

type bob = {
    foo : string ;
    bar : int := 0 ;
}

let x = { foo = "hello" }

(and maybe even some kind of 'nodefaults' annotation when defining 'x'
if I want to make sure I've explicitly defined every field.)


So what advice / solutions do other people have / use?

Thanks.

Jeffrey Cook
Ph.D. Candidate, Electrical Engineering
University of Illinois at Urbana-Champaign
jeffrey.j.cook@gmail.com


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Records with default values
  2005-03-23 19:41 Records with default values Jeffrey Cook
@ 2005-03-23 20:25 ` Martin Jambon
  0 siblings, 0 replies; 2+ messages in thread
From: Martin Jambon @ 2005-03-23 20:25 UTC (permalink / raw)
  To: Jeffrey Cook; +Cc: caml-list

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




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-03-23 20:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-23 19:41 Records with default values Jeffrey Cook
2005-03-23 20:25 ` [Caml-list] " Martin Jambon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).