More intuitive syntax for "if case"

Maybe instead of:

if case 1...3 = x { … } // why is there an assignment here?

You could have:

if match x case 1...3 { … }

Wouldn't this be more readable? Note that it avoids the assignment operator, which doesn't make sense when you don't have parameter binding in the pattern.

1 Like

Hi @Amir_Michail,

This is a topic with a lot of history that you can find by searching these forums:
https://forums.swift.org/search?q=if%20case%20syntax

If I recall correctly (you'll find the exact words somewhere in the reading above), the last time the core team or language steering group weighed in, the position was that we would be interested in pattern matching/binding (including case paths, for example) being explored holistically with an overarching vision, rather than tackled part by part.

Some of this work is made more complicated now because we must consider how to handle values of noncopyable type. Overall, it is not a project that can be undertaken without significant engineering effort.

1 Like

Thanks for the history, Xiaodi. As this forum's history grows longer and longer, it's increasingly valuable to have people doing the work of being the community’s memory.

I do agree with the sentiment of the OP: Swift’s pattern matching features are IMO underused because of the syntax, which harms both discoverability and readability. I hope the team has a chance to look at the big picture one day, difficult though that work will be.

4 Likes

The post xwu is referring to is here:

2 Likes