-Wall
を使用すると、コードが読みにくくなる可能性があると思います。特に、それがいくつかの算術をしている場合。
他のいくつかの例では、の使用が-Wall
読みやすさの悪い変更を示唆しています。
(^)
-Wall
指数の型署名が必要です
このコードを考えてみましょう:
norm2 x y = sqrt (x^2 + y^2)
main = print $ norm2 1 1
これにより-Wall
、次のような2つの警告が表示されます。
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)
はいいことではありません。
すべてのトップレベルに型署名が必要です
速くて汚いコードを書くとき、それは迷惑です。最大で1つまたは2つのデータ型が使用されている単純なコードの場合(たとえば、私はDouble
sでのみ作業することを知っています)、どこにでも型署名を書き込むと、読み取りが複雑になる可能性があります。上記の例では、型アノテーションがないことを示す2つの警告があります。
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
気を散らすものとして、それらの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
確認したいコードを使用しています。