Swift: `where let x = …` to bind variables in where clauses attached to cases
| Originator: | rix.rob | ||
| Number: | rdar://19415539 | Date Originated: | 08-Jan-2015 02:56 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 6.1.1 (6A2008a) |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
Sometimes when pattern matching a case C you then also need to perform some test on the matched value x. If that test takes the form f(x) != nil then you may also want to bind the result of f(x), and currently that means you have to either call f twice or rewrite your case to use an if statement after destructuring.
That is, currently you have to write this:
switch v {
case let C(x):
if let y = f(x) { return y }
else { return nil }
default:
return nil
}
This can complicate control flow substantially since you may need to explicitly handle the non-binding case instead of just letting a default case catch anything failing the test.
I propose allowing where clauses to bind constants, clarifying and simplifying control flow:
switch v {
case let C(x) where let y = f(x):
return y
default:
return nil
}
Steps to Reproduce:
N/A
Expected Results:
N/A
Actual Results:
N/A
Regression:
N/A
Notes:
N/A
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!