Friday 15 August 2014

haskell - Sum Type Vs Record of Maybes -



haskell - Sum Type Vs Record of Maybes -

a problem i'm facing when designing info type in haskell, either utilize sum type or records of maybe eithers.

a simple illustration modelize fx operation, spot or forward, difference presence or not of "maturity" date ( 1 way using sum type explicitely specify if it's spot or forward.

data amount = amount { amount :: double, currency :: string } info fx = spot { trandate :: day, soldamount :: amount, boughtamount :: amount } | forwards { trandate :: day, paidamount :: amount, boughtamount :: amount , maturity :: day}

another way have maturity `maybe'

data fx = fx { trandate :: day , soldamount :: amount , boughtamount :: amount , maturity (maybe day) }

or else

i don't recommend working sum types have named fields. unsafe accessors exist on 1 of branches. , aren't dry if have repeated fields.

but instead of putting maybe within of record, define wrapping record, this:

data spot = spot { trandate :: day , soldamount :: amount , boughtamount :: amount } info forwards = forwards { spot :: spot , maturity :: day }

and perhaps hasspot typeclass well, both spot , forward implement.

but hard set spot , forwards values in same collection. perhaps 1 utilize type (maybe day, spot) in case.

the "wrap it" approach of reply doesn't generalize more 1 optional field, however.

haskell

No comments:

Post a Comment