[Haskell-beginners] How do I do inheritance in haskell?

Dan Serban dserban01 at gmail.com
Thu May 8 08:58:28 UTC 2014


>The functions for generating the lists of bids and asks are essentially the same.

Create a typeclass umbrella for them.
Your option 3 looks good, but it doesn't go far enough.

Whenever you're presented with an opportunity to "harden" your design
by encoding problem domain invariants into the type system, you should
seize it - on principled grounds.

This goes back to one of the unique selling point of Haskell, which is
that by careful type design you can eliminate entire classes of bugs.

I tend to favor designs that look like this:

newtype AskVolume = AskVolume Double deriving (Show)
newtype AskPrice  = AskPrice  Double deriving (Show)

newtype BidVolume = BidVolume Double deriving (Show)
newtype BidPrice  = BidPrice  Double deriving (Show)

data AskOrder = AskOrder { aprice :: AskPrice, avolume :: AskVolume }
deriving (Show)
data BidOrder = BidOrder { bprice :: BidPrice, bvolume :: BidVolume }
deriving (Show)

data OrderBook = OrderBook { bids :: [BidOrder], asks :: [AskOrder] }
deriving (Show)

This is Haskell, so there are likely opportunities to move even more
stuff into the type system, but I'm not aware of them.

-Dan


More information about the Beginners mailing list