This is a fair objection. Actually, in Kotlin and TypeScript, this operation can be done and as you said after assignment it becomes optional again. (I found it after this comment)
Anyway, there is still possibility to be source breaking change.
(EDIT: First three patterns would not be source breaking change because x
still can work as String?
, but last two patterns would be source breaking)
let x: String? = "Hello"
func foo(_ x: String?) { /* ... */ }
func foo(_ x: String) { /* ... */ }
if x != nil {
print(x!) // after x != nil starts inferring, is this possible?
print(x ?? "Empty") // after x != nil starts inferring, is this possible?
print(x?.count) // after x != nil starts inferring, is this possible?
foo(x) // after x != nil starts inferring, which foo is called?
_ = x.map{$0} // after x != nil starts inferring, which is called? (Optional.map or String.map)
}
I agree inferencing is cool, but it cannot stay cool because it has too many limitation.
struct Foo {
var x: Int? {
return Bool.random() ? 42 : nil
}
func test() {
if x != nil {
// can x be used as `Int` ?
}
}
}