Swift should allow “switch” and “if” in expressions
| Originator: | jlieske | ||
| Number: | rdar://17246104 | Date Originated: | 09-Jun-2014 10:58 PM |
| Status: | Duplicate | Resolved: | |
| Product: | OS X SDK | Product Version: | Xcode6 beta: Version 6.0 (6A215l) |
| Classification: | Enhancement | Reproducible: | Always |
The Swift language supports many of the principles of functional programming, such as higher-order functions, immutable values, and pattern matching. One gap in the functional support is for conditional expressions.
Swift includes the ternary “? :” operator from C, but that operator does not support the “if let” test with binding that is so useful. Swift only allows “if” or “if let” in a statement, not an expression. Likewise, Swift only supports “switch” in a statement.
I propose adding syntax to Swift that allows conditional expressions with the full binding support of “if” and “let”. As a strawman, the syntax could look like the following:
let result = (if let myValue = someOptional {myValue.calculateSomething()}
else {calculateSomethingElse()})
let result = (switch someEnum {
case let .Success(value): .Some(value)
case let .Error(error): nil
})
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!
Apple closed as duplicate of rdar://16169366
A more longwinded argument for switch expressions, with some arguments regarding syntax: http://openradar.appspot.com/17258614