Swift Pattern Matching with Extra Conditions, and the Value of Syntax Summaries

Natalia Panferova writes about Swift enum pattern matching with extra conditions and goes over:

  • switch-case statements,
  • switch-case statements with where clauses,
  • for-in loops with where clauses,
  • while loops with extra conditions (case let matching),
  • if-case statements.
  • (Missing, for completion: catch ... where in try-catch blocks.)

I believe there’s tremendous value in summaries like these to learn the Swift programming language and its syntax: these short summaries show a slice of different aspects of the language in close proximity.

The Swift language book tells you all about the structural programming features one after another in the Control Flow chapter: if, for, while, etc.

It also has a section on where, but that’s limited to the switch conditional statement.

The section on for loops doesn’t mention where clauses at all! You need to go to the Language Reference part of the book, where the formal grammar is explained, to read that a where-clause? is allowed.

for-in-statement → for case? pattern in expression where-clause? code-block

So a post like Natalia’s reminds us about the similarity of different aspects of the Swift programming language.

It’s zooming in on where-clauses, and so the reader gets to know a different “view” into the syntax as a whole that is different from the book’s presentation.

This should be very valuable to get from beginner-level understanding of “I can do things in Swift” to a deeper understanding of recognizing similarities across different aspects of the programming language.

These should, in my opinion, be included in the language’s handbook to help with “deep learning”. Until then, I’m glad we have posts like Natalia’s!