18
考虑这个例子:Haskell中:违约约束键入
applyKTimes :: Integral i => i -> (a -> a) -> a -> a
applyKTimes 0 _ x = x
applyKTimes k f x = applyKTimes (k-1) f (f x)
applyThrice :: (a -> a) -> a -> a
applyThrice = applyKTimes 3
在applyThrice
的3
由GHC默认为一个Integer
所示与-Wall
编译时:
Warning: Defaulting the following constraint(s) to type 'Integer'
'Integral t'
arising from a use of 'applyKTimes'
所以我想这Integer
是默认Integral a => a
。
- 有没有办法为其他约束定义“默认类型”?
- 正在使用默认类型的不良做法? (它使用
-Wall
时抱怨..)