HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Request an account if you don't have one.

Default values in records

Categories: FAQ | Idioms

1 Question

Is there a way to declare default value of some fields when defining a new record type?

2 Anwser

You cannot associate default values with a type, but you can define as many record values as you like and use them as basis for updating elements.

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 statically checked by the compiler.

3 Source

Retrieved from "http://haskell.cs.yale.edu/haskellwiki/Default_values_in_records"

This page has been accessed 451 times. This page was last modified 23:07, 28 April 2009. Recent content is available under a simple permissive license.