|
| 1 | +# Abolish `ImplicitlyUnwrappedOptional` type |
| 2 | + |
| 3 | +* Proposal: [SE-0054](0054-abolish-iuo.md) |
| 4 | +* Author: [Chris Willmore](http://github.com/cwillmor) |
| 5 | +* Status: **Awaiting review** |
| 6 | +* Review Manager: [Chris Lattner](https://github.com/lattner) |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +This proposal seeks to remove the `ImplicitlyUnwrappedOptional` type from the |
| 11 | +Swift type system and replace it with an IUO attribute on declarations. |
| 12 | +Appending `!` to the type of a Swift declaration will give it optional type and |
| 13 | +annotate the declaration with an attribute stating that it may be implicitly |
| 14 | +unwrapped when used. |
| 15 | + |
| 16 | +Swift-evolution thread: ["Abolish IUO Type"](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160314/012752.html) |
| 17 | + |
| 18 | +## Motivation |
| 19 | + |
| 20 | +The `ImplicitlyUnwrappedOptional` ("IUO") type is a valuable tool for importing |
| 21 | +Objective-C APIs where the nullability of a parameter or return type is |
| 22 | +unspecified. It also represents a convenient mechanism for working through |
| 23 | +definite initialization problems in initializers. However, IUOs are a |
| 24 | +transitional technology; they represent an easy way to work around un-annotated |
| 25 | +APIs, or the lack of language features that could more elegantly handle certain |
| 26 | +patterns of code. As such, we would like to limit their usage moving forward, |
| 27 | +and introduce more specific language features to take their place. Except for a |
| 28 | +few specific scenarios, optionals are always the safer bet, and we’d like to |
| 29 | +encourage people to use them instead of IUOs. |
| 30 | + |
| 31 | +This proposal seeks to limit the adoption of IUOs to places where they are |
| 32 | +actually required, and put the Swift language on the path to removing |
| 33 | +implicitly unwrapped optionals from the system entirely when other technologies |
| 34 | +render them unnecessary. It also completely abolishes any notion of IUOs below |
| 35 | +the type-checker level of the compiler, which will substantially simplify the |
| 36 | +compiler implementation. |
| 37 | + |
| 38 | +## Proposed solution |
| 39 | + |
| 40 | +In this proposal, we continue to use the syntax `T!` for declaring implicitly |
| 41 | +unwrapped optional values in the following locations: |
| 42 | + |
| 43 | +* property and variable declarations |
| 44 | +* initializer declarations |
| 45 | +* function and method declarations |
| 46 | +* subscript declarations |
| 47 | +* parameter declarations (with the exception of vararg parameters) |
| 48 | + |
| 49 | +However, the appearance of `!` at the end of a property or variable |
| 50 | +declaration's type no longer indicates that the declaration has IUO type; |
| 51 | +rather, it indicates that (1) the declaration has optional type, and (2) the |
| 52 | +declaration has an attribute indicating that its value may be implicitly |
| 53 | +forced. (No human would ever write or observe this attribute, but we will |
| 54 | +refer to it as `@_autounwrapped`.) Such a declaration is referred to henceforth |
| 55 | +as an IUO declaration. |
| 56 | + |
| 57 | +Likewise, the appearance of `!` at the end of the return type of a function |
| 58 | +indicates that the function has optional return type and its return value may |
| 59 | +be implicitly unwrapped. The use of `init!` in an initializer declaration |
| 60 | +indicates that the initializer is failable and the result of the initializer |
| 61 | +may be implicitly unwrapped. In both of these cases, the `@_autounwrapped` |
| 62 | +attribute is attached to the declaration. |
| 63 | + |
| 64 | +A reference to an IUO variable or property prefers to bind to an optional, but |
| 65 | +may be implicitly forced (i.e. converted to the underlying type) when being |
| 66 | +type-checked; this replicates the current behavior of a declaration with IUO |
| 67 | +type. Likewise, the result of a function application or initialization where |
| 68 | +the callee is a reference to an IUO function declaration prefers to retain its |
| 69 | +optional type, but may be implicitly forced if necessary. |
| 70 | + |
| 71 | +If the expression can be explicitly type checked with a strong optional type, |
| 72 | +it will be. However, the type checker will fall back to forcing the optional if |
| 73 | +necessary. The effect of this behavior is that the result of any expression |
| 74 | +that refers to a value declared as `T!` will either have type `T` or type `T?`. |
| 75 | +For example, in the following code: |
| 76 | + |
| 77 | +```Swift |
| 78 | +let x: Int! = 5 |
| 79 | +let y = x |
| 80 | +let z = x + 0 |
| 81 | +``` |
| 82 | + |
| 83 | +… `x` is declared as an IUO, but because the initializer for `y` type checks |
| 84 | +correctly as an optional, `y` will be bound as type `Int?`. However, the |
| 85 | +initializer for `z` does not type check with `x` declared as an optional |
| 86 | +(there's no overload of `+` that takes an optional), so the compiler forces the |
| 87 | +optional and type checks the initializer as `Int`. |
| 88 | + |
| 89 | +This model is more predictable because it prevents IUOs from propagating |
| 90 | +implicitly through the codebase, and converts them to strong optionals, the |
| 91 | +safer option, by default. |
| 92 | + |
| 93 | +An IUO variable may still be converted to a value with non-optional type, |
| 94 | +through either evaluating it in a context which requires the non-optional type, |
| 95 | +explicitly converting it to a non-optional type using the `as` operator, |
| 96 | +binding it to a variable with explicit optional type, or using the force |
| 97 | +operator (`!`). |
| 98 | + |
| 99 | +Because IUOs are an attribute on declarations rather than on types, the |
| 100 | +`ImplicitlyUnwrappedOptional` type, as well as the long form |
| 101 | +`ImplicitlyUnwrappedOptional<T>` syntax, is removed. Types with nested IUOs are |
| 102 | +no longer allowed. This includes types such as `[Int!]` and `(Int!, Int!)`. |
| 103 | + |
| 104 | +Type aliases may not have IUO information associated with them. Thus the |
| 105 | +statement `typealias X = Int!` is illegal. This includes type aliases resulting |
| 106 | +from imported `typedef` statements. For example, the Objective-C type |
| 107 | +declaration |
| 108 | + |
| 109 | +```Objective-C |
| 110 | +typedef void (^ViewHandler)(NSView *); |
| 111 | +``` |
| 112 | +
|
| 113 | +... is imported as the Swift type declaration |
| 114 | +
|
| 115 | +```Swift |
| 116 | +typealias ViewHandler = (NSView?) -> () |
| 117 | +``` |
| 118 | + |
| 119 | +Note that the parameter type is `NSView?`, not `NSView!`. |
| 120 | + |
| 121 | +## Examples |
| 122 | + |
| 123 | +```Swift |
| 124 | +func f() -> Int! { return 3 } // f: () -> Int?, has IUO attribute |
| 125 | +let x1 = f() // succeeds; x1: Int? == 3 |
| 126 | +let x2: Int? = f() // succeeds; x2: Int? = .some(3) |
| 127 | +let x3: Int! = f() // succeeds; x3: Int? = .some(3), has IUO attribute |
| 128 | +let x4: Int = f() // succeeds; x4: Int = 3 |
| 129 | +let a1 = [f()] // succeeds; a: [Int?] = [.some(3)] |
| 130 | +let a2: [Int!] = [f()] // illegal, nested IUO type |
| 131 | +let a3: [Int] = [f()] // succeeds; a: [Int] = [3] |
| 132 | + |
| 133 | +func g() -> Int! { return nil } // f: () -> Int?, has IUO attribute |
| 134 | +let y1 = g() // succeeds; y1: Int? = .none |
| 135 | +let y2: Int? = g() // succeeds; y2: Int? = .none |
| 136 | +let y3: Int! = g() // succeeds; y3: Int? = .none, has IUO attribute |
| 137 | +let y4: Int = g() // traps |
| 138 | +let b1 = [g()] // succeeds; b: [Int?] = [.none] |
| 139 | +let b2: [Int!] = [g()] // illegal, nested IUO type |
| 140 | +let b3: [Int] = [g()] // traps |
| 141 | + |
| 142 | +func p<T>(x: T) { print(x) } |
| 143 | +p(f()) // prints "Optional(3)"; p is instantiated with T = Int? |
| 144 | + |
| 145 | +if let x5 = f() { |
| 146 | + // executes, with x5: Int = 3 |
| 147 | +} |
| 148 | +if let y5 = g() { |
| 149 | + // does not execute |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +## Impact on existing code |
| 154 | + |
| 155 | +These changes will break existing code; as a result, I would like for them to |
| 156 | +be considered for inclusion in Swift 3. This breakage will come in two forms: |
| 157 | + |
| 158 | +* Variable bindings which previously had inferred type `T!` from their binding |
| 159 | + on the right-hand side will now have type `T?`. The compiler will emit an |
| 160 | + error at sites where those bound variables are used in a context that demands |
| 161 | + a non-optional type and suggest that the value be forced with the `!` |
| 162 | + operator. |
| 163 | + |
| 164 | +* Explicitly written nested IUO types (like `[Int!]`) will have to be rewritten |
| 165 | + to use the corresponding optional type (`[Int?]`) or non-optional type |
| 166 | + (`[Int]`) depending on what's more appropriate for the context. However, most |
| 167 | + declarations with non-nested IUO type will continue to work as they did |
| 168 | + before. |
| 169 | + |
| 170 | +* Unsugared use of the `ImplicitlyUnwrappedOptional` type will have to be |
| 171 | + replaced with the postfix `!` notation. |
| 172 | + |
| 173 | +It will still be possible to declare IUO properties, so the following deferred |
| 174 | +initialization pattern will still be possible: |
| 175 | + |
| 176 | +```Swift |
| 177 | +struct S { |
| 178 | + var x: Int! |
| 179 | + init() {} |
| 180 | + func initLater(x someX: Int) { x = someX } |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +I consider the level of breakage resulting from this change acceptable. Types |
| 185 | +imported from Objective-C APIs change frequently as those APIs gain nullability |
| 186 | +annotations, and that occasionally breaks existing code too; this change will |
| 187 | +have similar effect. |
| 188 | + |
| 189 | +## Alternatives considered |
| 190 | + |
| 191 | +* Continue to allow IUO type, but don't propagate it to variables and |
| 192 | + intermediate values without explicit type annotation. This resolves the issue |
| 193 | + of IUO propagation but still allows nested IUO types, and doesn't address the |
| 194 | + complexity of handling IUOs below the Sema level of the compiler. |
| 195 | + |
| 196 | +* Remove IUOs completely. Untenable due to the prevalence of deferred |
| 197 | + initialization and unannotated Objective-C API in today's Swift ecosystem. |
0 commit comments