Missing space after tilde `~` [GHC-27207]
The ~ character can be used for multiple purposes:
- For type equality in a function signature:
a ~ b - As a binary operator:
(~) :: a -> b -> c - For defining non-strict fields in the presence of the
StrictDataextension:data Foo = MkFoo Int ~Char - For making irrefutable pattern matches lazy again in the presence of the
Strictextension:f ~x = 5 - As a lazy pattern to turn refutable patterns into irrefutable patterns:
case undefined of ~(a,b) -> length [a,b]
If no space is placed between ~ and the expression that follows it, this is interpreted as the last bullet; in expression contexts, the lazy pattern is not allowed. The most likely case is that you wish to use ~ as a binary operator, in which case a space is needed following it.
Examples
Missing space after tilde `~`
There is no space after the tilde character ~.
Error Message
MissingSpaceLazyPattern.hs:11:9: error:
Lazy pattern in expression context: ~1
Did you mean to add a space after the '~'?
|
11 | f x = x ~1
| ^^
MissingSpaceLazyPattern.hs
Before
module MissingSpaceLazyPattern where
(~) :: Int -> Int -> Bool
(~) a b = a == b
infixl 5 ~
f :: Int -> Bool
f x = x ~1
After
module MissingSpaceLazyPattern where
(~) :: Int -> Int -> Bool
(~) a b = a == b
infixl 5 ~
f :: Int -> Bool
f x = x ~ 1