Operator whitespace [GHC-40798]

GHC distinguishes between a few different occurences for operators depending on whitespace. An operator can occur in the following ways:

a ! b   -- a loose infix occurrence
a!b     -- a tight infix occurrence
a !b    -- a prefix occurrence
a! b    -- a suffix occurrence

A loose infix occurrence will always be treated as an operator, whereas any of the remaining cases may be redefined by a future language extension.

Consider -XBangPatterns

a ! b = <rhs>  -- Infix (!)
f !a = <rhs>   -- Bang pattern

the above code is possible today, whereas

a +b = <rhs>

would break if a language extension that introduces a prefix occurence for plus is introduced.

The documentation for the -Woperator-whitespace flag can be found in the GHC user guide

Examples

operator-whitespace

Error Message


Main.hs:9:10: warning: [-Woperator-whitespace] [GHC-40798]
    The suffix use of a ‘+’ might be repurposed as special syntax
      by a future language extension.
    Suggested fix: Add whitespace around ‘+’.
  |
9 | f a b = a+ b -- this one is a warning
  |          ^

Main.hs:12:11: warning: [-Woperator-whitespace] [GHC-40798]
    The prefix use of a ‘+’ might be repurposed as special syntax
      by a future language extension.
    Suggested fix: Add whitespace around ‘+’.
   |
12 | g a b = a +b -- this one is also a warning
   |           ^

Main.hs:15:10: warning: [-Woperator-whitespace] [GHC-40798]
    The tight infix use of a ‘+’ might be repurposed as special syntax
      by a future language extension.
    Suggested fix: Add whitespace around ‘+’.
   |
15 | h a b = a+b -- this one as well
   |          ^
Main.hs
Before
{-# OPTIONS -Woperator-whitespace #-}

module Main where

main :: IO ()
main = print "notice the usage of whitespace below"

f :: Num a => a -> a -> a
f a b = a+ b -- this one is a warning

g :: Num a => a -> a -> a
g a b = a +b -- this one is also a warning

h :: Num a => a -> a -> a
h a b = a+b -- this one as well
After
{-# OPTIONS -Woperator-whitespace #-}

module Main where

main :: IO ()
main = print "notice the usage of whitespace below"

f :: Num a => a -> a -> a
f a b = a + b