【发布时间】:2010-05-18 23:53:30
【问题描述】:
考虑这个例子:
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..时确实会抱怨)
【问题讨论】: