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

Yitzchak Gale gale at sefer.org
Mon Jul 18 09:25:42 CEST 2011


David Virebayre wrote:
> I'm not sure you read his post entirely.
>
> Here's his and function :
>> (and) c1 c2  =  Contract { currency = undefined, payments = undefined, contracts = [c1, c2] }
>
> Notice how he's setting currency and payments to undefined, since he
> needs only the contracts field for subcontracts.

Ah, right, I didn't notice that. Not a good idea. Your design
is better then.

Another possibility would be Maybe Currency and Maybe Double.
That often comes out simpler - but not always. If the case
with Currency and Payments is a totally separate case than
when there are subcontracts, with different logic, then David's
design is best. If the cases are usually handled pretty much
in the same way with special handling for when one or more
fields is missing, then Maybe is best.

-Yitz



More information about the Beginners mailing list