Inconsistent behavior with NilLiteralConvertable in Swift
| Originator: | bwhiteley | ||
| Number: | rdar://22761995 | Date Originated: | 2015-09-18 |
| Status: | Open | Resolved: | |
| Product: | Swift | Product Version: | Swift2 |
| Classification: | Reproducible: | Always |
Is this a bug or feature?
struct Foo : CustomStringConvertible {
let text:String
init(text:String) {
self.text = text
}
var description:String { return self.text }
}
extension Foo : NilLiteralConvertible {
init(nilLiteral: ()) {
self.init(text:"null")
}
}
func f1() -> Foo? {
return nil // This returns .None
}
func f2() -> Foo? {
return true ? nil : Foo(text: "bar") // This returns .Some(Foo)
}
let one = f1()
let two = f2()
if case .None = one {
print("one is .None")
}
if case .None = two { // false
print("two is .None")
}
if let two = two {
print("two is \(two.text)") // We see this!
}
// To achieve what you probably intended, use .None instead of nil
func f2_() -> Foo? {
return true ? .None : Foo(text: "bar") // This returns .None
}
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!