<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 27, 2015, at 2:47 PM, John McCall <<a href="mailto:rjmccall@apple.com" class="">rjmccall@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 27, 2015, at 10:37 AM, Joe Groff via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><ul class="" style="box-sizing: border-box; padding: 0px 0px 0px 2em; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><li class="" style="box-sizing: border-box;"><p class="" style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">Getters and setters can be written using dotted syntax within the back-ticks:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre class="" style="box-sizing: border-box; overflow: auto; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; padding: 16px; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-wrap: normal; word-break: normal;"><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> specificTitle <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> button<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>`currentTitle<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span>` <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// has type () -> String?</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> otherTitle <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> UIButton<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>`currentTitle<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span>` <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// has type (UIButton) -> () -> String?</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> setTintColor <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> button<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>`tintColor<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">set</span>` <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// has type (UIColor!) -> ()</span></pre></div><p class="" style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">The same syntax works with subscript getters and setters as well, using the full name of the subscript:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px;"><pre class="" style="box-sizing: border-box; overflow: auto; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; padding: 16px; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-wrap: normal; word-break: normal;"><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> Matrix {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">subscript</span> (row row: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> [<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Double</span>] {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">set</span> { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
}
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> getRow <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> someMatrix<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>`<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">subscript</span>(row:)<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">get</span>` <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// has type (Int) -> () -> [Double]</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> setRow <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> someMatrix<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>`<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">subscript</span>(row:)<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">set</span>` <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// has type (Int) -> ([Double]) -> ()</span></pre></div></li></ul></div></div></div></blockquote><div class="">At least as far as pure Swift is concerned, for unapplied access, like `UIButton.currentTitle`, I think it would be more consistent with the way method references works for that to give you the getter (or lens) without decoration. instance.instanceMethod has type Args -> Ret, and Type.instanceMethod has type Self -> Args -> Ret; by analogy, since instance.instanceProperty has type Ret or inout Ret, it's reasonable to expect Type.instanceProperty to have type Self -> [inout] Ret. Forming a getter or setter partially applied to an instance feels unmotivated to me—{ button.currentTitle } or { button.currentTitle = $0 } already work, and are arguably clearer than this syntax.</div><div class=""><br class=""></div><div class="">I acknowledge that this leaves forming selectors from setters out to dry, but I feel like that's something that could be incorporated into a "lens" design along with typed selectors. As a rough sketch, we could say that the representation of @convention(selector) T -> inout U is a pair of getter/setter selectors, and provide API on Selector to grab the individual selectors from that, maybe Selector(getterFor: UIView.currentTitle)/(setterFor: UIView.currentTitle). I don't think get/set is a good interface for working with Swift properties, so I don't like the idea of building in language support to codify it beyond what's needed for ObjC interaction.</div></div></div></blockquote><div class=""><br class=""></div>I know this might be too early, but: what syntax are we thinking of for lenses? We might want to design this with future consistency in mind.</div></div></div></blockquote><br class=""></div><div>Vaguely, I think it could look something like this. You could define a lens function by having it return `inout`. Calling the function produces an lvalue whose access nests within the accesses of its input `inout` parameters, if any, allowing for things like:</div><div><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div>var localVar = 1</div><div>let localRef: () -> inout Int = { &localVar }</div><div><br class=""></div><div>func second(inout array: [Int]) -> inout Int {</div><div> return &array[1]</div><div>}</div><div><br class=""></div><div>// Maybe you can define an inout function with accessors too</div><div>func fahrenheit(inout celsius: Double) -> inout Double {</div><div> get {</div><div> return celsius * 9/5 + 32</div><div> }</div><div> set {</div><div> celsius = (newValue - 32) * 5/9</div><div> }</div><div>}</div></blockquote><br class=""><div class="">and you could access the unapplied lens for an instance property using `Type.property` syntax, analogous to how `Type.method` works. I feel like if we did that, then it would obviate the need for explicit `property.get` or `property.set` for most native Swift uses, though maybe not ObjC interop uses.</div><div class=""><br class=""></div><div class="">-Joe</div></body></html>