<p dir="ltr">Dear haskellers,</p>
<p dir="ltr">What is the difference between writing<br></p>
<p dir="ltr">data Task = GroceryTask String | LaundryTask Int</p>
<p dir="ltr">doTask :: Task -> IO ()<br>
doTask (GroceryTask s) = print "Going to " ++ s<br>
doTask (LaundryTask n) = print (show n ++ " pieces washed"<br></p>
<p dir="ltr">and<br></p>
<p dir="ltr">class Task a where<br>
work :: a -> IO ()</p>
<p dir="ltr">data GroceryTask = GroceryTask String<br>
data LaundryTask = LaundryTask Int</p>
<p dir="ltr">instance Task GroceryTask where ..</p>
<p dir="ltr">instance Task LaundryTask where ..</p>
<p dir="ltr">doTask :: Task a => a -> IO ()<br>
doTask = work<br><br></p>
<p dir="ltr">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?<br></p>
<p dir="ltr">Happy new year,<br>
Hon</p>