Looking for a comprehensive/up-to-date list of "everywhere I can use the where clause"

It's shown only for switch/case control flow in the the swift programming language

I want to see all the places where the where-clause can be used

Type constraints

enum MyEnum<T> where T: Codable { ... }
func foo<T>(_ x: T) where T: Codable { ... }

for...in loops

for x in 0...10 where x % 2 == 0 { ... }

switch cases

switch (-1, 1) {
case let (x, y) where x == y: ...
default: ...
}

catch statements

enum MyError: Error { case foo(Int, Int) }
func bar() throws { throw MyError.foo(2, 3) }

do {
  try bar()
} catch let MyError.foo(x, y) where x + y == 5 {
  ...
}

In if and guard statements you can just use a comma instead of where.

6 Likes

Couple of more places of "Where usage in Swift"

Not sure if Array.first(where:) and Array.contains(where:) should count? Those are not where keyword, but just parameter label, the source show:

@inlinable public func first(where predicate: (Element) throws -> Bool) rethrows -> Element?
@inlinable public func contains(where predicate: (Element) throws -> Bool) rethrows -> Bool

where is not escape with back tick...and compiler do not object.

this compile fine:

func cannotusewherehere(for default: Int) { }

so I guess you can use reserved keyword for func parameters.

So where as func parameter label definitely should not count as where (keyword) usage.

You can also look for where-clause in Summary of the Grammar.

yes (SE-0001).

2 Likes