[Haskell-cafe] Design Question - Functions taking 'subtype' like arguments but has to be restricted to one type

MarLinn monkleyon at googlemail.com
Sat Aug 6 14:23:00 UTC 2016

>     /If all carts behave the same and contents don't matter/ just
>     store /PlainBasket/s, i.e. a /Basket/ without the annotation.
> Can you clarify the quoted statement.  If I were to make store 
> PlainBaskets in the Cart, where would be annotating the Item as 
> Changeble/Unchangable?

Well /contents don't matter, /so there is no annotation once you enter a 

     -- A/PlainBasket/  is to a/Basket/  as a/PlainItem/  is to an/Item./
     data PlainBasket = PlainBasket { plainBasketName :: String, plainBasketItem :: PlainItem }
     data Basket c = Basket { basketName :: String, basketItem :: Item c }

     newtype Cart = Cart [PlainBasket]
     addBasketToCart :: Basket c -> Cart -> Cart
     addBasketToCart Basket{..} (Cart cart) = Cart $ newPlainBasket:cart
       where newPlainBasket = PlainBasket { plainBasketName = basketName
					 , plainBasketItem = plainItem basketItem

The newtype protects your cart from arbitrary changes, so frozen baskets 
are still protected. But the annotations are discarded. So naturally, 
you can not recover changeability and you shouldn't allow arbitrary 
mapping. If you need one of these, this is not the right option. It 
really depends on what you actually need.

Of course you could add a changeability field to /PlainBasket/s and thus 
store the information at runtime again. But Theodores /Either/-approach 
or variations of the class I propose in my the third option are more 
elegant forms of that.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160806/b6bf282f/attachment.html>

More information about the Haskell-Cafe mailing list