"where" on a switch case with multiple items is misleading


(Amir Michail) #1

Consider:

switch x {
  case a, b where …: ...
}

this currently means:

switch x {
  case a where true, b where …: ...
}

Most people would probably think the “where" applies to both a and b.

To avoid this confusion, maybe if “where" is used for one item then it should be required for all items in that case?


(Lucas Neiva) #2

Requiring “where" for all patterns seems like a good idea. I’ve run into this before.

Instinctively I tried to group using ( ), but then you’re matching tuples:

  case (a, b) where <where-expression>:

Having a way to apply the where-clause to all patterns would be nice. Maybe like this:

  case let a, b where all <where-expression>:

···

Consider:

switch x {
case a, b where …: ...
}

this currently means:

switch x {
case a where true, b where …: ...
}

Most people would probably think the “where" applies to both a and b.

To avoid this confusion, maybe if “where" is used for one item then it should be required for all items in that case?