我发现我可以执行1 :: Product Int
,结果会得到Product {getProduct = 1}
。
Product
是在Data.Monoid
中定义的一个newtype
。然后我尝试像这样定义我的自己的newtype
:
newtype Stuff a = Stuff {getStuff :: a} deriving (Show)
但是如果我尝试执行 1 :: Stuff Int
,就会出现错误:
<interactive>:20:1: error:
* No instance for (Num (Stuff Int)) arising from the literal `1'
* In the expression: 1 :: Stuff Int
In an equation for `it': it = 1 :: Stuff Int
我需要给 a
加上 Num
约束吗?为什么这样做不起作用?