[Haskell-cafe] Idiomatic ways to make all instances of a certain class also instances of another?

Tim Cowlishaw tim at timcowlishaw.co.uk
Wed Jul 27 08:58:17 CEST 2011


On Tue, Jul 26, 2011 at 11:14 PM, Alexander Solla <alex.solla at gmail.com> wrote:

> data OrderType = Market Size | Limit LimitPrice Expiration Size | Stop
> (Either Percent Price)
> newtype Sell = Sell OrderType
> newtype Buy = Buy OrderType
> newtype Order = Order (Either Buy Sell)

> size :: Order -> Int
> size (Order (Left (Buy (Market s))) = s
> size (Order (Left (Buy (Limit _ _ s))) = s
> etc.

Aah, thank you - this is really neat. So now, I can write (for
instance) an Eq instance for OrderType and use deriving (Eq) on the
newtypes that wrap it, and my Order can be a concrete type, but still
encapsulates all the different types of order.

Thank you!

Tim

Thank you



More information about the Haskell-Cafe mailing list