<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 Mar 22, 2017, at 3:30 PM, Brent Royal-Gordon <<a href="mailto:brent@architechies.com" class="">brent@architechies.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" 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 Mar 22, 2017, at 11:27 AM, Matthew Johnson 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=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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; float: none; display: inline !important;" class="">One option would be to include `get` and `set` methods on the key path types. That would allow us to write the subscripts in the standard library (if it is allowed to extend Any) and keep<span class="Apple-converted-space"> </span></span><i class="" style="font-family: Helvetica; font-size: 12px; font-variant-caps: 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;">all</i><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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; float: none; display: inline !important;" class=""> of the magic in the key path types themselves. I think I would like that approach.</span></div></blockquote></div><div class=""><br class=""></div><div class="">This is not a good option. Although we Swift users may write `get` and `set` accessors, the real primitives in Swift are `get` and `materializeForSet`, which is basically "return a pointer that can be modified until another call is made which finalizes the set". `materializeForSet`'s call sequence is too low-level to be written manually; without adding other features to the language, we're basically left with either calling a method that calls a closure with an `inout` parameter, or using a subscript.</div></div></div></blockquote><div><br class=""></div><div>This is what I had in mind (synthesized by the compiler):</div><div><br class=""></div><div>extension WritableKeyPath {</div><div> func get(from root: Root) -> Value { … }</div><div> func set(to value: Value, on root: inout Root) { … }</div><div>}</div><div><br class=""></div><div>And something like this in the standard library:</div><div><br class=""></div><div>extension Any {</div><div> subscript<Root: Self, Value>(path: WritableKeyPath<Root, Value>) -> Value { </div><div> get {</div><div> return path.get(from: self)</div><div> }</div><div> set {</div><div> path.set(to: value, on: &self)</div><div> }</div><div> }</div><div>}</div><div><br class=""></div><div>Is there a reason this wouldn’t work? I’m only presenting it for the sake of discussion. I don’t have a strong opinion about it one way or the other. The nice thing about it is that it keeps the magic in the key path types.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">The ownership manifesto includes a generator feature which could, some day, make it possible to write a method wrapping `materializeForSet`. But subscripts allow us to do this today, and it's *perfectly* natural to think of key paths as being a subscript; subscripts are for accessing values inside an instance, and a key path addresses a value inside an instance, too.</div></div></div></blockquote><div><br class=""></div><div>I definitely think the subscript syntax should be supported. I’m only suggesting an option that might allow us to keep all of the compiler magic in the key path types.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">I guess I just don't understand why people seem so eager to change this syntax. The biggest complaint seems to be that it's too lightweight and natural; they're worried they might not realize they're using this big, scary new feature. But this feature simply *isn't* big and scary! It's new right now, but in a few years it's going to feel very natural.</div></div></div></blockquote><div><br class=""></div><div>I’m not eager. I’m just discussing options that are acceptable to me because a lot of people seem to want something different. I’m happy with the syntax included in the proposal as well.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">I can't find it now, but I once read someone state that this is part of what happened to C++: Every time someone proposed a new feature, people were worried that they would use it by accident, so they insisted that it have an unmistakable, obvious syntax so you would know you were using it. The result is that now, almost every syntax in C++ is shouting at you that you're using feature X, Y, or Z, and you can't hear your own code over the din.</div></div></div></blockquote><div><br class=""></div><div>A good cautionary tale. I’m pretty neutral between the syntax in the proposal and the # sigil idea. Regardless of which we go with, I think there is some value in exploring how we might keep the compiler magic in the key path types.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">Key paths are about as nonthreatening as a syntax can be. They overload some things, but the type checker will catch most overloading mistakes. Other than that, there's just no justification for the level of anxiety people seem to have. They don't break type safety, they don't involve unusual or non-obvious control flow, they don't trap, and they can be explained in a couple of sentences. If ever there was a candidate for a lightweight syntax, this is it. </div></div></div></blockquote><div><br class=""></div>Agree. They can also be conceptualized as compiler synthesized static properties if we move forward with the syntax in the current proposal. There is nothing wrong with this model aside from a minimal risk of ambiguity (which I don’t think is worth worrying about).</div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">As the protest sign says: "I want YOU to stop being afraid". I have yet to be convinced that this feature is too dangerous or mistake-prone to allow the simple, natural syntax the authors propose. If I'm wrong, then by all means show me I'm wrong, but I just don't see it.</div></div></div></blockquote><div><br class=""></div><div>I agree that the fear seems to be overblown. That said, key paths are synthesizing special values in a way that has some similarity to #selector. It has also been pointed out that # could resolve some issues we have right now with unbound methods. If it makes people feel better to use # to access unbound members and it solves a problem, maybe it’s worth considering.</div><div><br class=""></div><div>Overall, I’m pretty neutral on this topic. I’m mostly trying to guide people away from discussing syntax options that I think are bad ideas. I guess that’s what you’re trying to do also. :)</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class="">
<span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: normal; border-spacing: 0px;"><div class=""><div style="font-size: 12px; " class="">-- </div><div style="font-size: 12px; " class="">Brent Royal-Gordon</div><div style="font-size: 12px; " class="">Architechies</div></div></span>
</div>
<br class=""></div></div></blockquote></div><br class=""></body></html>