[Haskell-beginners] Some guidance

Frerich Raabe raabe at froglogic.com
Tue Jul 2 09:13:51 CEST 2013


Hi Peter,

Am 7/2/2013 12:52 AM, schrieb Peter Hall:
> The other problem I'm facing is with name collisions for record
> accessors. There are two versions of MtGoxTicker from different services
> - one with a subset of the properties, which is supposed to be faster
> (it isn't, but that isn't the point). The data types are here
> https://github.com/peterjoel/auto-trader/blob/6974d66ae51459479c19be291d075bbdeb718b53/AutoTrader/MtGox/Types.hs.
> One is commented one out while I decide what to do. What is the best way
> to model those records to avoid collisions, while not being confusing to
> users of the library? I am very tempted to use type classes, but that
> feels naughty. Using unique prefixes seems bad too - it would be nice
> for some code to be able to use them interchangeably if they don't need
> all the fields.

This sounds like a good case for two separate modules:

AutoTrader.MtGox.Ticker.Full would have a data Ticker = Ticker { .. } 
which is the commented-out MtGoxTickerFull, and 
AutoTrader.MtGox.Ticker.Fast would balso have a 'data Ticker = ...', 
which would be your MtGoxTicker.

This would allow you to use the same field names without getting 
clashes, users of your library could choose hwo to import the types, 
i.e. what prefix to use - and people using no fields from the full 
ticker could switch their code by changing something like

   import qualified AutoTrader.MtGox.Ticker.Full

to

   import qualified AutoTrader.MtGox.Ticker.Fast

As a side note, I think that in the vast majority of cases where you 
think that a type class is be a good solution - it's not. :-}

-- 
Frerich Raabe - raabe at froglogic.com
www.froglogic.com - Multi-Platform GUI Testing




More information about the Beginners mailing list