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
時抱怨..)