GHCの-Wtype-defaultsを解決するにはちゃんと型付けするしかないかなあ
Test.hs
main :: IO ()
main = print 10
このコードを-Wtype-defaults
付きでコンパイルすると、以下の警告が出る。
(-Wtype-defaults
はGHC-8.0.2ではデフォルトで無効)
$ stack runghc -- -Wtype-defaults Test.hs
Test.hs:2:8: warning: [-Wtype-defaults]
• Defaulting the following constraints to type ‘Integer’
(Show a0) arising from a use of ‘print’ at Test.hs:2:8-15
(Num a0) arising from the literal ‘10’ at Test.hs:2:14-15
• In the expression: print 10
In an equation for ‘main’: main = print 10
10
10が型推論で(Num a, Show a) => a
に推論されているからだ。
GHCは10を最終的に単相型で型付けする必要がある……はずなので、デフォルトのInteger
として型付けされていて、
このままだとパフォーマンスに影響が出る。(Integer
よりInt
のが速い)
型を付けて解決する。
Test.hs
main :: IO ()
main = print (10 :: Int)
他にいい解決方法ないの?