我认为使用-Wall
可能会导致代码可读性降低。特别是,如果它正在做一些算术。
其他一些示例,其中使用-Wall
建议修改可读性较差。
(^)
with-Wall
需要指数的类型签名
考虑这段代码:
norm2 x y = sqrt (x^2 + y^2)
main = print $ norm2 1 1
它-Wall
给出了两个这样的警告:
rt.hs:1:18:
Warning: Defaulting the following constraint(s) to type `Integer'
`Integral t' arising from a use of `^' at rt.hs:2:18-20
In the first argument of `(+)', namely `x ^ 2'
In the first argument of `sqrt', namely `(x ^ 2 + y ^ 2)'
In the expression: sqrt (x ^ 2 + y ^ 2)
(^(2::Int)
到处写而不是(^2)
不好。
所有顶层都需要类型签名
在编写快速而肮脏的代码时,这很烦人。对于简单的代码,最多使用一种或两种数据类型(例如,我知道我只使用Double
s),在任何地方编写类型签名可能会使阅读变得复杂。在上面的示例中,仅针对缺少类型签名有两个警告:
rt.hs:1:0:
Warning: Definition but no type signature for `norm2'
Inferred type: norm2 :: forall a. (Floating a) => a -> a -> a
...
rt.hs:2:15:
Warning: Defaulting the following constraint(s) to type `Double'
`Floating a' arising from a use of `norm2' at rt.hs:2:15-23
In the second argument of `($)', namely `norm2 1 1'
In the expression: print $ norm2 1 1
In the definition of `main': main = print $ norm2 1 1
作为一种干扰,其中之一是指与需要类型签名的行不同的行。
中间计算的类型签名Integral
是必要的
这是第一个问题的一般情况。考虑一个例子:
stripe x = fromIntegral . round $ x - (fromIntegral (floor x))
main = mapM_ (print . stripe) [0,0.1..2.0]
它给出了一堆警告。到处fromIntegral
都可以转换回Double
:
rt2.hs:1:11:
Warning: Defaulting the following constraint(s) to type `Integer'
`Integral b' arising from a use of `fromIntegral' at rt2.hs:1:11-22
In the first argument of `(.)', namely `fromIntegral'
In the first argument of `($)', namely `fromIntegral . round'
In the expression:
fromIntegral . round $ x - (fromIntegral (floor x))
fromIntegral
每个人都知道在 Haskell 中需要多久......
像这样的情况更多,数字代码可能会因为满足-Wall
要求而变得不可读。但我仍然使用-Wall
我想确定的代码。