也就是说haskell中有wtype默认值?

mm5n2pyu  于 2023-02-23  发布在  其他
关注(0)|答案(1)|浏览(118)

此代码将导致警告

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

有人能解释一下警告的意思吗?

fykwrbwg

fykwrbwg1#

你从来没有指定round将返回什么类型,所以编译器必须选择一个类型,这样它才能构造一个实际的参数传递给fromIntegeral。警告消息只是告诉你,编译器从所有其他类型中选择了Integer,这些类型具有Integral示例,也可以工作。

相关问题