[Haskell-cafe] default values in a record structure

Luke Palmer lrpalmer at gmail.com
Tue Apr 28 18:18:39 EDT 2009


On Tue, Apr 28, 2009 at 4:09 PM, Vasili I. Galchin <vigalchin at gmail.com>wrote:

> Hello,
>
>      Is there anyway when defining a dat type record struct to indicate
> default values for some of the

fields?


The usual pattern is to use a default record, and specialize it:

data Foo = Foo { bar :: Int, baz :: Int, quux :: Int }

fooDefault = Foo { bar = 1, baz = 2, quux = 3 }

newRecord = fooDefault { quux = 42 }

If you only want some of the fields to be defaulted, you can make them
undefined in the default record... unfortunately this won't be checked by
the compiler.

Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090428/ae385056/attachment.htm


More information about the Haskell-Cafe mailing list