- What is your evaluation of the proposal?
Big +1. I think this makes the language feel more consistent and expressive. The return keyword does not communicate anything of value in these contexts. I think most Swift programmers will enjoy the sugar once they are aware it is available and start using it.
I very much agree with the decision to limit this sugar to single-expression bodies. I think the contexts in which single-expression returns can be confusing are limited to contexts in which statements are also present. This proposal nicely avoids those issues.
The initializer case is of little value on its own, but I don't think including it is problematic and it increases overall consistency.
- Is the problem being addressed significant enough to warrant a change to Swift?
Yes. It's a relatively minor, but very nice and convenient form of syntactic sugar. Removing meaningless syntactic details from our code increases overall clarity.
- Does this proposal fit well with the feel and direction of Swift?
Very much so. Swift embraces a very thoughtful balance between clarity and convenience. In cases where syntax adds no meaningful clarity for experienced programmers we are usually able to elide it. This proposal is very much inline with this direction.
- If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
Yes. I have used several lanaguages with similar features. I think this proposal captures the benefits of the sugar without the potential for confusion that can be introduced when it is mixed with statements.
I want to call out the alternative sugar for function syntax that was inspired by other languages:
func square(of x: Int) -> Int = x * x
I agree with everything the proposal authors have to say about this alternative in particular and want to point out that the difference in number of characters in the syntax is exactly one (the =
is replaced by an opening brace and a closing brace is added on the end):
func square(of x: Int) -> Int = x * x
// vs
func square(of x: Int) -> Int { x * x }
I admit that the former syntax feels lighter despite only one character difference. However, I think the latter is more appropriate for Swift. In particular, I think this syntax is a bit confusing when used with getters, especially get-only computed properties where it cannot be used without braces because it would conflict with stored properties:
// stored property
var x: Int = 42
// computed property using brace syntax
var x: Int { 42 }
// a computed property using `=` syntax needs to use braces and say `get`
// in order to distinguish itself from a stored property
// IMO this issue is a dealbreaker for `=` syntax in the context of getters
var x: Int { get = 42 }
Swift has very intentionally placed all code bodies inside of braces. I think this is a good decision and we should stick with that direction.
- How much effort did you put into your review? A glance, a quick reading, or an in-depth study?
I have followed all the discussion threads and read the final proposal (as well as current reviews).