Swift: Optionals guarded by ternary operators require explicit unwrapping
| Originator: | rix.rob | ||
| Number: | rdar://17496219 | Date Originated: | 28-Jun-2014 01:05 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode6-Beta2 (6A216f) |
| Classification: | Serious Bug | Reproducible: | Always |
Summary:
An Optional type guarded by a ternary operator is known to be safe, but it still requires explicit unwrapping.
Steps to Reproduce:
1. Write the following type:
struct Thing {
var next: Thing? {
get { return _next?[0] }
// Note that newValue, which is of type `Thing?`, is safe to access in the then clause of the ternary operator
set { _next = newValue ? [ newValue ] : nil }
}
var _next: Thing[]?
}
2. Note that newValue is correctly guarded by the ternary operator (which tests it for nil-ness).
3. Try to compile it.
Expected Results:
I expected it to compile—after all, newValue in [ newValue ] can be implicitly unwrapped since this usage is guarded by the ternary operator.
Actual Results:
It doesn’t compile; you have to do [ newValue! ], explicitly unwrapping it.
Regression:
N/A
Notes:
The ternary operator doesn’t provide a way to bind to a new name, and if let foo = bar { … } is not an expression. Thus, since there is no explicit promotion of newValue from Thing? to the implicitly unwrapped Thing!, you have to deal with this detail yourself.
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!