<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=""><div><blockquote type="cite" class=""><div class="">On Nov 15, 2017, at 10:00 PM, Brent Royal-Gordon &lt;<a href="mailto:brent@architechies.com" class="">brent@architechies.com</a>&gt; wrote:</div><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class="">On Nov 14, 2017, at 11:29 PM, Chris Lattner via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="highlight highlight-source-swift" style="font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; box-sizing: border-box; margin-bottom: 16px; color: rgb(36, 41, 46); font-family: -apple-system, system-ui, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;; font-size: 16px; font-variant-ligatures: normal; orphans: 2; widows: 2;"><pre class="" style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size: 13.6px; margin-top: 0px; margin-bottom: 0px; word-wrap: normal; padding: 16px; overflow: auto; line-height: 1.45; background-color: rgb(246, 248, 250); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;"><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">extension</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">PyVal</span> {
  <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">subscript</span>(dynamicMember member<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">String</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-&gt;</span> PyVal {
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">get</span> {
      <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> result <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">PyObject_GetAttrString</span>(borrowedPyObject, member)<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">!</span>
      <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">return</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">PyRef</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">owned</span>: result)  <span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box;">//</span> PyObject_GetAttrString returns +1 result.</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"></span>    }
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">set</span> {
      <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">PyObject_SetAttrString</span>(borrowedPyObject, member,
                             newValue.<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">toPython</span>().<span class="pl-smi" style="box-sizing: border-box;">borrowedPyObject</span>)
    }
  }
}</pre></div></div></blockquote></div><div class="">This looks great for Python, but let's talk about some other languages for a moment.</div><div class=""><br class=""></div><div class="">* Ruby and Perl don't have the "call a method by fetching a closure property and invoking it" behavior you're relying on here. Instead, Ruby has a syntax for settable "overloads" of methods (i.e. you can write `def someMember` and `def someMember= (newValue)`), while Perl supports lvalue methods (but sometimes uses getter and setter method pairs instead). How do you envision these behaviors being bridged to Swift? I worry that this protocol may not be sufficient, and that we may need a design which can distinguish between looking up methods and looking up properties.</div></div></div></blockquote><div><br class=""></div><div>I’m not sure without more description, but it seems like these are both general property models which can be implemented with this protocol as is. &nbsp;The getter would call someMember, and the setter would call someMember=. &nbsp;What complication are you anticipating?</div><div><br class=""></div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">* Ruby looks up members using symbols, which essentially play the same role as selectors in Objective-C—they're uniqued strings which are used for fast member dispatch. In some cases, you might see non-negligible speed improvements by only looking up the symbol once. Is there a way this design could accommodate that?&nbsp;</div></div></div></blockquote><div><br class=""></div><div>Yes, I think that this is very much in scope for the DynamicCallable proposal. &nbsp;We talked about this in the context of Squeak (another smalltalky language), and it fits naturally with the design. &nbsp;I’ll add that when I have time to revise the pitch.</div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">* Generally, you've talked about properties (in this proposal) and methods (in the `DynamicCallable` proposal), but what about subscripts? Obviously you can just specify a `subscript(Pythonable) -&gt; PyVal` on `PyVal` for the simple case, but what if the subscript takes multiple indices or has labels? Do we need a `DynamicSubscriptable` protocol?</div></div></blockquote><div><br class=""></div><div>Subscripts can already be varargs, so the only reason we’d need a DynamicSubscriptable proposal is if there were another language that allowed keywords on subscript indices. &nbsp;If so, then yes, we’d need that.</div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">* Let's step away from bridging entirely and just think about Swift for a moment. &nbsp;There are cases where we'd like to make *semi*-dynamic proxies which wrap another type and allow operations based on what's statically known about that type. Think, for example, of the appearance proxy in UIKit: This is an object attached to UIView subclasses which lets you (in essence) set default values for all instances. We currently just pretend it's an instance of `Self`, which mostly works because of Objective-C, but a Swift-native version would probably prefer to return a `UIAppearance&lt;Self&gt;` object which used its knowledge of `Self` to expose `Self`'s properties on itself. Is there a way we could design this feature, or a related feature, to cover that kind of use case? That is, to allow a limited set of keys—perhaps even key-path-based when you want static control—with a different type for each key, *or* to allow any key with some common type, depending on your type's needs?</div></div></blockquote><div><br class=""></div><div>I wouldn’t be at all surprised if this was possible, but given that this type erases everything that goes through the proxy, and given that it doesn’t provide type checking, it isn’t clear to me that you’d get a really great answer.</div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">* Actually, for that matter, let's talk about key paths. In principle, you can already think of member lookup in Swift—or at least property and subscript lookup—as though it always worked by constructing a key path and using `subscript(keyPath:)` to access it. Is there some way we could model this feature as extending the set of keys available on a given type—perhaps in a way that allowed compile-time-limited and strongly-typed sets of keys, like I mention with the `UIAppearance` example, in addition to the open-ended, type-erased sets you need—and then looking things up by key path? (Sorry if this is a little vague—I know very little about how key paths are implemented.)</div></div></blockquote><div><br class=""></div>I don’t know.</div><div><br class=""><blockquote type="cite" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">* An implementation-level question about Swift: Internally, the compiler seems to be moving towards thinking of parameter labels as part of the identifier, rather than having them label the individual arguments. How do you see that jibing with what you're proposing here?</div></div></blockquote><br class=""></div><div>This is part of the DynamicCallable proposal that I will be revising as mentioned above, it isn’t related to the member lookup side of the equation.</div><div><br class=""></div><div>-Chris</div><div><br class=""></div><br class=""></body></html>