[swift-evolution] [Pitch] Make `return` optional in computed properties for a single case

Brent Royal-Gordon brent at architechies.com
Fri May 27 18:15:42 CDT 2016


> The idea is simple:
> 
> 	• Can we make return keyword optional in cases like this?
> 	• Shouldn’t this behave like @autoclosure or @noescape?

This actually doesn't have anything to do with @autoclosure or @noescape. Any one-expression closure can omit the `return` statement and have an inferred return type.

> type A {
>     var characters: [Character] = …
>     var string: String { String(self.characters) }
>     var count: Int { 42 }
> }

Despite those inaccuracies, I do think that it's a good idea to permit single-expression accessors to omit the `return` statement; it will make them much less clunky. I would even extend this to cases where you use the `get` keyword:

	var string: String {
		get { String(self.characters) }
		set { characters = Array(newValue.characters) }
	}

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list