<div dir="ltr">Certain languages allow the programmer to avoid creating backing variables for getters and setters by using this syntax:<br><br>class Foo {<br> init(x: Double) {<br> self.x = x<br> }<br> var x: Double {<br> get<br> set<br> }<br><div> // alternatively var x: Double { get; set }<br></div>}<br><br><div>and generating code equivalent to this:<br></div><div><br>class Foo {<br> init(x: Double) {<br> _x = x<br> }<br> var _x: Double<br> var x: Double {<br> get {<br> return _x<br> }<br> set {<br> _x = newValue<br> }<br> }<br>}<br><br></div><div>This notation decreases verbosity and reduces the chance of incorrectly implementing the pattern.<br><br></div><div>In the following case, the computed property 'x' can only be set in the initializer.<br><br>class Foo {<br> init(x: Double) {<br> self.x = x<br> }<br> var x: Double { get }<br>}<br><br></div><div>Alternatively, the following syntax can be used to avoid using an initializer:<br><br>class Foo {<br> var x: Double { get } = 1.0<br>}</div><div><br></div><div>Before looking into the nuances of this syntax (regarding struct/enum properties, access control, attributes, etc.) I would like to ask the community if this feature would be a good fit for Swift.<br clear="all"></div><div><br>-- <br><div class="gmail_signature">Nathan<br></div>
</div></div>