Suppose I have a lambda expression in my program like:
\x -> f $ x + 1
and I want to specify for type safety that x must be an Integer. Something like:
-- WARNING: bad code
\x::Int -> f $ x + 1
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can just write
\x -> f $ (x::Int) + 1instead. Or, perhaps more readable,\x -> f (x + 1 :: Int). Note that type signatures generally encompass everything to their left, as far left as makes syntactic sense, which is the opposite of lambdas extending to the right.The GHC extension
ScopedTypeVariablesincidentally allows writing signatures directly in patterns, which would allow\(x::Int) -> f $ x + 1. But that extension also adds a bunch of other stuff you might not want to worry about; I wouldn’t turn it on just for a syntactic nicety.