[Haskell-beginners] Can fields in a record be optional?

David Place d at vidplace.com
Mon Jul 18 00:18:55 CEST 2011


The way I often do this is to create an "ur" instance where all the fields have default values.  Then to create an instance, I just do a "record update" of this instance.  For example:

data Contract =  Contract {
                    currency  :: Currency
                  , payments  :: Double
                  , contracts :: [Contract]
                }
                deriving (Show)

urContract = Contract { currency = undefined, payments = undefined, contracts = [] }

one    :: Currency -> Contract
one c  =  urContract { currency = c, payments = 1}

and          :: Contract -> Contract -> Contract
(and) c1 c2  =  urContract { contracts = [c1, c2] }

____________________
David Place   
Owner, Panpipes Ho! LLC
http://panpipesho.com
d at vidplace.com



On Jul 17, 2011, at 6:03 PM, Costello, Roger L. wrote:

> Hi Folks,
> 
> Can fields in a record be made "optional"? 
> 




More information about the Beginners mailing list