[swift-evolution] [pitch] replace (if/guard) case = with ~=

Erica Sadun erica at ericasadun.com
Mon Oct 3 20:31:43 CDT 2016


Resolved: `if case 1...10 = myNumber { ... }` is an abomination.

It confuses inexperience developers. At least those few who are 
aware of its existence. Using the assignment operator for pattern 
matching adds insult to injury. 

I far prefer
    `if case .failure(let error) ~= result { ... } `
to
    `if case .failure(let error) = result {...}`

Though worthy, this isn't a popular pattern.  A highly unscientific survey of 
gist reveal:

* Gists using "if case": 94
* Gists that could use "if let" instead of "if case": Approximately 94-ish
* Gists using "guard case" (with significant overlap with "if case"): 54
* Gists that could use "guard let" or "guard x != nil" or "guard x == .enumeration" instead: About 54-ish
* Standard library: 1 use of "guard case", 5 uses of "if case".

Note:

* I love `guard case`/`if case` for `Result` enumerations
* I  love`for case let x? in [optionals]`. 

I don't expect changing `=` to `~=` would make a huge difference in adoption 
but it would satisfy  my inner code critic. Changing it would be breaking
but as far as I can tell, it wouldn't really break *that* *much* *code*

-- E


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161003/b8f736d6/attachment.html>


More information about the swift-evolution mailing list