<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 11, 2015, at 11:20 AM, Nathan Yee via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Certain languages allow the programmer to avoid creating backing variables for getters and setters by using this syntax:<br class=""><br class="">class Foo {<br class=""> init(x: Double) {<br class=""> self.x = x<br class=""> }<br class=""> var x: Double {<br class=""> get<br class=""> set<br class=""> }<br class=""><div class=""> // alternatively var x: Double { get; set }<br class=""></div>}<br class=""><br class=""><div class="">and generating code equivalent to this:<br class=""></div><div class=""><br class="">class Foo {<br class=""> init(x: Double) {<br class=""> _x = x<br class=""> }<br class=""> var _x: Double<br class=""> var x: Double {<br class=""> get {<br class=""> return _x<br class=""> }<br class=""> set {<br class=""> _x = newValue<br class=""> }<br class=""> }<br class="">}<br class=""></div></div></div></blockquote><div><br class=""></div><div>Hi Nathan,</div><div><br class=""></div><div>How is this different than just declaring “var x : Double”? It is an important part of swift’s design that stored and computed properties are equivalent to external uses of an API.</div><div><br class=""></div><div>-Chris</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">This notation decreases verbosity and reduces the chance of incorrectly implementing the pattern.<br class=""><br class=""></div><div class="">In the following case, the computed property 'x' can only be set in the initializer.<br class=""><br class="">class Foo {<br class=""> init(x: Double) {<br class=""> self.x = x<br class=""> }<br class=""> var x: Double { get }<br class="">}<br class=""><br class=""></div><div class="">Alternatively, the following syntax can be used to avoid using an initializer:<br class=""><br class="">class Foo {<br class=""> var x: Double { get } = 1.0<br class="">}</div><div class=""><br class=""></div><div class="">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" class=""></div><div class=""><br class="">-- <br class=""><div class="gmail_signature">Nathan<br class=""></div>
</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=RC5Cq0zAxCHc1sM9Uy3-2BojrrUAw-2F96zH69NULNHPvCvfPAddsnEgyr0IONVzbI2IGxbFsdhL5tb1kMAQozItWeAKA7oAtlXnhaMW62dyt15g17nYJjNuFNsq1mjipdCauB6IqVUaBOYNSsBZSUkrX0O-2B20Q9KYYjjA8xynr-2Fgf4llqsnYuMejMLm-2Fjr0e5HGp2De1rmGzsDkN0Yn22rupWP-2Bk-2BC-2FKcB-2BSErJPiAhuQM-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>