Impact on style of GHC -Wall

前端 未结 6 427
囚心锁ツ
囚心锁ツ 2021-02-02 10:16

It is considered good practice to enable GHC warnings with -Wall. However, I\'ve found out that fixing those warnings has a negative effect for some types of code c

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-02 10:43

    Example 1:

    I have re-learned to write parsers in Applicative style -- they are much more concise. Eg, instead of:

    funCallExpr :: Parser AST
    funCallExpr = do
        func <- atom
        token "("
        arg <- expr
        token ")"
        return $ FunCall func arg
    

    I instead write:

    funCallExpr :: Parser AST
    funCallExpr = FunCall <$> atom <* token "(" <*> expr <* token ")"
    

    But what can I say, if you don't like the warning, disable it as it suggests.

    Example 2:

    Yeah I find that warning a bit irritating as well. But it has saved me a couple times.

    It ties into naming conventions. I like to keep modules pretty small, and keep most imports qualified (except for "notation" imports like Control.Applicative and Control.Arrow). That keeps the chances of name conflict low, and it just makes things easy to work with. hothasktags makes this style tolerable if you are using tags.

    If you are just pattern matching on a field with the same name, you can use -XNamedFieldPuns or -XRecordWildCards to reuse the name:

    data Foo = Foo { baz :: Int, bar :: String }
    
    -- RecordWildCards
    doubleBaz :: Foo -> Int
    doubleBaz (Foo {..}) = baz*baz
    
    -- NamedFieldPuns
    reverseBar :: Foo -> String
    reverseBar (Foo {bar}) = reverse bar
    

    Another common convention is to add a hungarian prefix to record labels:

    data Foo = Foo { fooBaz :: Int, fooBar :: String }
    

    But yeah, records are no fun to work with in Haskell. Anyway, keep your modules small and your abstractions tight and this shouldn't be a problem. Consider it as a warning that says simplifyyyy, man.

提交回复
热议问题