|
| 1 | +# Removing `var` from Function Parameters |
| 2 | + |
| 3 | +* Proposal: [SE-0003](https://github.com/apple/swift-evolution/blob/master/proposals/0003-remove-var-parameters-patterns.md) |
| 4 | +* Author(s): [David Farler](https://github.com/bitjammer) |
| 5 | +* Status: **Accepted** |
| 6 | +* Review manager: [Joe Pamer](https://github.com/jopamer) |
| 7 | + |
| 8 | +## Note |
| 9 | + |
| 10 | +This proposal underwent some major changes from its original form. See |
| 11 | +the end of this document for historical information and why this |
| 12 | +proposal changed. |
| 13 | + |
| 14 | +## Introduction |
| 15 | + |
| 16 | +There has been some confusion of semantics when a function parameter is |
| 17 | +marked as `inout` compared to `var`. Both give a mutable local copy of a |
| 18 | +value but parameters marked `inout` are automatically written back. |
| 19 | + |
| 20 | +Function parameters are immutable by default: |
| 21 | + |
| 22 | +```swift |
| 23 | +func foo(i: Int) { |
| 24 | + i += 1 // illegal |
| 25 | +} |
| 26 | + |
| 27 | +func foo(var i: Int) { |
| 28 | + i += 1 // OK, but the caller cannot observe this mutation. |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +Here, the *local copy* of `x` mutates but the write does not propagate back to |
| 33 | +the original value that was passed, so the caller can never observe the change |
| 34 | +directly. For that to happen to value types, you have to mark the parameter |
| 35 | +with `inout`: |
| 36 | + |
| 37 | +```swift |
| 38 | +func doSomethingWithInout(inout i: Int) { |
| 39 | + i = 2 // This will have an effect on the caller's Int that was passed. |
| 40 | +} |
| 41 | + |
| 42 | +var x = 1 |
| 43 | +print(x) // 1 |
| 44 | + |
| 45 | +doSomethingWithVar(x) |
| 46 | +print(x) // 1 |
| 47 | + |
| 48 | +doSomethingWithInout(&x) |
| 49 | +print(x) // 2 |
| 50 | +``` |
| 51 | + |
| 52 | +## Motivation |
| 53 | + |
| 54 | +Using `var` annotations on function parameters have limited utility, |
| 55 | +optimizing for a line of code at the cost of confusion with `inout`, |
| 56 | +which has the semantics most people expect. To emphasize the fact these |
| 57 | +values are unique copies and don't have the write-back semantics of |
| 58 | +`inout`, we should not allow `var` here. |
| 59 | + |
| 60 | +In summary, the problems that motivate this change are: |
| 61 | + |
| 62 | +- `var` is often confused with `inout` in function parameters. |
| 63 | +- `var` is often confused to make value types have reference semantics. |
| 64 | +- Function parameters are not refutable patterns like in *if-*, |
| 65 | + *while-*, *guard-*, *for-in-*, and *case* statements. |
| 66 | + |
| 67 | +## Design |
| 68 | + |
| 69 | +This is a trivial change to the parser. In Swift 2.2, a deprecation |
| 70 | +warning will be emitted while in Swift 3 it will become an error. |
| 71 | + |
| 72 | +## Impact on existing code |
| 73 | + |
| 74 | +As a purely mechanical migration away from these uses of `var`, a temporary |
| 75 | +variable can be immediately introduced that shadows the immutable copy in all of |
| 76 | +the above uses. For example: |
| 77 | + |
| 78 | +```swift |
| 79 | +func foo(i: Int) { |
| 80 | + var i = i |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +However, shadowing is not necessarily an ideal fix and may indicate an |
| 85 | +anti-pattern. We expect users of Swift to rethink some of their existing |
| 86 | +code where these are used but it is not strictly necessary to react to |
| 87 | +this language change. |
| 88 | + |
| 89 | +## Alternatives considered |
| 90 | + |
| 91 | +This proposal originally included removal of `var` bindings for all |
| 92 | +refutable patterns as well as function parameters. |
| 93 | + |
| 94 | +[Original SE-0003 Proposal](https://github.com/apple/swift-evolution/blob/8cd734260bc60d6d49dbfb48de5632e63bf200cc/proposals/0003-remove-var-parameters-patterns.md) |
| 95 | + |
| 96 | +Removal of `var` from refutable patterns was reconsidered due to the |
| 97 | +burden it placed on valid mutation patterns already in use in Swift 2 |
| 98 | +code. You can view the discussion on the swift-evolution mailing list |
| 99 | +here: |
| 100 | + |
| 101 | +[Initial Discussion of Reconsideration](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160118/007326.html) |
| 102 | + |
| 103 | +The rationale for a final conclusion was also sent to the |
| 104 | +swift-evolution list, which you can view here: |
| 105 | + |
| 106 | +[Note on Revision of the Proposal](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/008145.html) |
0 commit comments