Prior to Swift’s open sourcing, SE-0003, which eliminated var from function parameters and pattern matching was accepted into the language for Swift 3.0.
Since then, the Swift community has pushed back and pushed back hard against this decision, particularly when using var in case pattern matching and if/guard/for statements.
Today, the Swift team issued a statement:
The core team met to discuss SE-0003, and we have a new conclusion, described below. As was mentioned on the other thread, this proposal is a bit unusual because it predates Swift open source, and thus didn’t go through the full evolution process, nor did it get the normal public review that would come with it.The core team broke this proposal down into a few different cases, each of which came to a different conclusion. To be clear up front, this is not a clear-cut topic, and there are opinions on all sides. Here are the decisions the core team came to, along with some rationale:
Parameter Lists‘var’ in a parameter list is problematic for a number of reasons:
- Parameter lists currently allow both “inout” and “var”, and real confusion occurs for some people learning swift that expect “var” to provide reference semantics.
- Parameter lists are not “patterns” in the language, so neither “let” nor “var” really make sense there. Parameters were patterns in a much older swift design, but that was eliminated a long time ago and this aspect never got reconsidered.
- “var” in a parameter list is odd because it is an implementation detail being foisted on the interface to the function. To be fair, we already have this with “API names” vs “internal names”, but this is a bigger semantic implementation detail.Conclusion: The core team decided that we should REMOVE “var” and “let” from parameter lists. “var” in a parameter list is convenient sugar that defines away some minor boilerplate (the shadowing copy), however, the cost benefit tradeoff doesn’t pay enough to keep it.
Patterns“if var”, “for var”, “if case var” etc all use the pattern grammar (aka “pattern matching” and “destructuring” features described in TSPL). We discussed whether to eliminate var from this, with much more mixed results:
- We currently have a duality across the language between var and let which is common, unifying, and works really well in practice. Eliminating var from pattern matching would make the language less uniform, because patterns wouldn’t align with var/let declarations.
- Working in Swift *demands* that you get an early grasp on what value semantics means, and “var x = y” is always a copy of the value. As such, based on the strong duality mentioned above, it is reasonable to expect people to learn that “if var x = y” produces a copy and not a reference binding.
- There certainly is confusion from people who expect “if var” to do a reference binding, despite the comment directly above. However, we feel that this is more of a point-in-time confusion than the parameter case, given that the parameter case exposes the ‘var’ as part of the function signature.Conclusion: The core team decided that we should KEEP “var” and “let” in patterns.
Case PatternsWe had an additional discussion focused specifically on whether it would make sense to ban var in case patterns, e.g.: case .Foo(var x):
- The core team agrees that this specific case is a common point of confusion, particularly because Swift doesn’t provide a way to get a mutable binding to the associated value in an enum.
- That said, making a special case for this would make Swift less regular.Conclusions:
- The core team decided to KEEP “var” here.
- We will stop leading people into it by eliminating the “note: change ‘let’ to ‘var’ to make it mutable” note that the compiler produces for patterns.Eliminating the note allows users sufficiently knowledgable about Swift to keep using it, but people who just mash the “fixit” button to silence the compiler won’t get surprising behavior.-Swift Core Team
In the end,
var will be removed from parameter lists, where it can confuse users with
inout functionality1 but remain available for most other use cases, eliminating the labor of constantly using
let binding followed by
Swift developers everywhere are celebrating this morning. Let us all raise a glass to the Swift Team in honor of this thoughtful reconsideration.
1 I am working on a proposal that will further simplify
inout by moving it from the label side of declarations to the type side. Watch this space.