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