Default values in records
From HaskellWiki
(Difference between revisions)
(taken from a post to Haskell-Cafe) |
m (typo) |
||
| Line 3: | Line 3: | ||
Is there a way to declare default value of some fields when defining a new record type? | Is there a way to declare default value of some fields when defining a new record type? | ||
| - | == | + | == Answer == |
You cannot associate default values with a type, | You cannot associate default values with a type, | ||
Current revision
1 Question
Is there a way to declare default value of some fields when defining a new record type?
2 Answer
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
- Haskell-Cafe on default values in a record structure
Categories: FAQ | Idioms
