此代码将导致警告
truncate' :: Maybe Double -> Int -> Maybe Double
truncate' Nothing _ = Nothing
truncate' (Just x) n = Just $ fromIntegral (round (x * t)) / t
where
t = 10 ^ n
我明白了
test/Spec.hs:9:31: warning: [-Wtype-defaults]
• Defaulting the following constraint to type ‘Integer’
Integral a0 arising from a use of ‘fromIntegral’
• In the first argument of ‘(/)’, namely
‘fromIntegral (round (x * t))’
In the second argument of ‘($)’, namely
‘fromIntegral (round (x * t)) / t’
In the expression: Just $ fromIntegral (round (x * t)) / t
有人能解释一下警告的意思吗?
1条答案
按热度按时间fykwrbwg1#
你从来没有指定
round
将返回什么类型,所以编译器必须选择一个类型,这样它才能构造一个实际的参数传递给fromIntegeral
。警告消息只是告诉你,编译器从所有其他类型中选择了Integer
,这些类型具有Integral
示例,也可以工作。