What is a good reason to use a type signature for functions when the compiler can infer the types

后端 未结 4 597
梦谈多话
梦谈多话 2021-01-17 09:44

I am trying to code using \'good Haskell style\' and so am trying to follow typical coding standards I find around. Also, compiling with -Wall and -Werror as I am used to w

4条回答
  •  失恋的感觉
    2021-01-17 10:07

    Your contrived example is really contrived, since the function body does not depend on the type of contents of the list. In this case it is indeed difficult to see what's the benefit of defining the type to be [String] -> ([String],[String]) instead of [a]->([a],[a])

    If you attempt to define a function that depends on the contents, you will see that the type definition is not the only thing you need to change. For example, changing a list for MArray is going to be far more involved, not just using a function that happens to have the same name in a different module. So qualifying the name during refactoring in a few narrow cases is not a good enough reason to not specify type signatures.

    Specifying the type tells the compiler a little bit of the intent. Then the compiler will be able to report the mismatch of the intent and the implementation.

提交回复
热议问题