[Pitch] Allow if/switch expressions in arbitrary expression positions

SE-0380 restricts if and switch expressions to assignments and returns. I'd like them wherever an ordinary expression is accepted, especially argument values and parenthesized postfix receivers:

animate(
  orientation: switch alignment {
  case .none: entityOrientation
  case .target: targetOrientation
  }
)
(switch alignment {
case .none: entityOrientation
case .target: targetOrientation
}).mirrorRotation(of: targetOrientation)

The latter is unambiguous because the parentheses delimit the switch. Today both require otherwise unnecessary intermediate values.

Parenthesized arbitrary expression positions were suggested during the original pitch. Would a focused follow-up allowing parenthesized if/switch expressions as primary expressions be welcome?

15 Likes

Strictly speaking no, as we could write a closure:

{
    switch alignment {
        case .none: entityOrientation
        case .target: targetOrientation
    }
}().mirrorRotation(of: targetOrientation)

but that feels awkward. +1 to allowing if, switch, and possibly do in arbitrary expression positions.


BTW, is it really ambiguous without parenthesis?

We have a precedent of:

sort { ... }.filter { ... }

then why not allowing:

switch x { ... }.mirrorRotation(...)

or even this:

if x { ... } else { ... }.mirrorRotation(...)

without parenthesis.

1 Like