6.2.15. Multi-way if-expressions¶
- MultiWayIf¶
- Since:
7.6.1
Allow the use of multi-way-
ifsyntax.
With MultiWayIf extension GHC accepts conditional expressions with multiple branches:
if | guard1 -> expr1 | ... | guardN -> exprN which is roughly equivalent to
case () of _ | guard1 -> expr1 ... _ | guardN -> exprN Multi-way if expressions introduce a new layout context. So the example above is equivalent to:
if { | guard1 -> expr1 ; | ... ; | guardN -> exprN } The following behaves as expected:
if | guard1 -> if | guard2 -> expr2 | guard3 -> expr3 | guard4 -> expr4 because layout translates it as
if { | guard1 -> if { | guard2 -> expr2 ; | guard3 -> expr3 } ; | guard4 -> expr4 } Layout with multi-way if works in the same way as other layout contexts, except that the semi-colons between guards in a multi-way if are optional. So it is not necessary to line up all the guards at the same column; this is consistent with the way guards work in function definitions and case expressions.
Note that multi-way if supports guards other than boolean conditions:
if | parseNumbers settings , Just (exponent, mantissa) <- decomposeNumber str , let (integralPart, fractionPart) = parse mantissa , integralPart >= 0 -> ... | otherwise -> ...