newtype behaviour in Haskell -
i have discovered can 1 :: product int , product {getproduct = 1} result.
product newtype defined in data.monoid. have tried defining own newtype :
newtype stuff = stuff {getstuff :: a} deriving (show) but if try 1 :: stuff int error :
<interactive>:20:1: error: * no instance (num (stuff int)) arising literal `1' * in expression: 1 :: stuff int in equation `it': = 1 :: stuff int do have put num constraint on a or something? why doesn't work?
you can 1 :: t if , if t instance of num. 1 :: product int works because product defines instance instance num => num (product a) (i.e. if a instance of num, product a instance of num).
do have put num constraint on or something?
you have define num instance stuff int (or better num => stuff a).
you can either manually (using instance) or automatically using deriving num , generalizednewtypederiving extension, define num instance num => stuff a acts num instance given a.
Comments
Post a Comment