How to define function signatures partially in Haskell?

后端 未结 6 1211
刺人心
刺人心 2021-02-03 17:54

Starting point:

fn :: [a] -> Int
fn = (2 *) . length

Let\'s say we only want to constrain the return value, then we could write:

6条回答
  •  灰色年华
    2021-02-03 18:24

    I think you wish for a pretty bad thing. Feature you want slightly increases handiness of type inference, especially for top-level functions. But signatures of top-level declarations represent essential design contracts. They are API, they are documentation, they are beacons for strangers foraying into your code, thus they have to be rock-solid and clear.

    Yes, haskell allows type constraints for return types. But this is mostly for temporary results in let-blocks. And yes, you may use

     f (x :: Int) = 2*x
    

    syntax with XScopedTypeVariables extension (yet it isn't applicable to point-free functions).

提交回复
热议问题