[Haskell-cafe] Data declaration vs type classes

Lian Hung Hon hon.lianhung at gmail.com
Fri Jan 8 09:55:45 UTC 2016


Dear haskellers,

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"

and

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
appropriate here?

Happy new year,
Hon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160108/58a010f2/attachment.html>


More information about the Haskell-Cafe mailing list