[Haskell-cafe] Data declaration vs type classes
Lian Hung Hon
hon.lianhung at gmail.com
Fri Jan 8 09:55:45 UTC 2016
What is the difference between writing
data Task = GroceryTask String | LaundryTask Int
doTask :: Task -> IO ()
doTask (GroceryTask s) = print "Going to " ++ s
doTask (LaundryTask n) = print (show n ++ " pieces washed"
class Task a where
work :: a -> IO ()
data GroceryTask = GroceryTask String
data LaundryTask = LaundryTask Int
instance Task GroceryTask where ..
instance Task LaundryTask where ..
doTask :: Task a => a -> IO ()
doTask = work
They seem to be similar functionality wise, except that one is on the data
level and another is on the class level. How should one go about deciding
to use data or class? Is there a semantic difference? Which is more
Happy new year,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe