
On Mon, Sep 22, 2008 at 02:38:32PM -0500, Mike Sullivan wrote:
Hi all,
I was wondering if there is syntactic sugar in Haskell for defining a default value for fields in a data type. For instance, say I have a type that is defined in record syntax:
type CustomerID = Int type Address = Maybe String
data Customer = Customer { customerID :: CustomerID , customerName :: String , customerAddress :: Address } deriving (Show)
Is there any way to define default values for some (or all) fields such that they may be omitted from a declaration, and still have it generate a valid object?
e.g.) a = Customer{customerID = 12, customerName="Bill"} -- I would like a{customerAddress} to default to Nothing (for instance).
It seems to me that this would be a nice feature to have, if it does not exist. Am I missing something?
Hi Mike, This feature doesn't exist, but it is easy to code it yourself by creating a record with default values in it: defaultCust = Customer 0 "" Nothing Then instead of saying a = Customer{...}, you can say a = defaultCust{customerID = 12, customerName="Bill"} and the remaining field will default to Nothing, since that field of defaultCust was not overridden. -Brent