<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 20, 2017, at 12:16 AM, David Hart via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">I’ve had a quick look into how your proposals will allow interop with other dynamic languages. It seems that the model that you chose, where calling a function is a two-step process (getting a <font face="Menlo" class="">DynamicCallable</font> function from a <font face="Menlo" class="">DynamicMemberLookupProtocol</font> type) fits Python like a glove, where everything (including functions) is a PyVal. But in other languages, like Ruby, this model makes less sense.</div></div></div></blockquote><div><br class=""></div>Right, this was also brought up in the DynamicCallable thread, the example given before was Squeak, another Smalltalky language.</div><div><br class=""></div><div>It turns out that the right ultimate design for DynamicCallable is probably to have two different entry points: one for direct-function-like calls and one for methods-with-keywords calls, e.g.:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>fn(arg: 1, arg2: 2) &nbsp; // function like</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>obj.method(arg: 1, arg2: 2) &nbsp; // method like</div><div><br class=""></div><div>It is straight-forward (and fits very very naturally into the Swift call model) to support the second one as an atomic thing, which is I think what you’re getting at. &nbsp;I plan to revise the DynamicCallable proposal to incorporate this directly into the model, but haven’t had time to do so yet.</div><div><br class=""></div><div>-Chris</div><div><br class=""></div><div><div><br class=""></div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">For example, here is how one uses the Ruby C API to call a method of an object:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">result&nbsp;=&nbsp;rb_funcallv(obj,&nbsp;rb_intern(“methodName"),&nbsp;arg_count,&nbsp;args);</font></div><div class=""><br class=""></div><div class="">And here is how one gets and sets instance variables:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">x&nbsp;=&nbsp;rb_iv_get(obj,&nbsp;"@x");<br class="">rb_iv_set(obj,&nbsp;"@x",&nbsp;x);</font></div><div class=""><br class=""></div><div class="">Moreover, Ruby allows classes to have instance variables with the same name as methods:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">class Foo</font><div class=""><font face="Menlo" class="">&nbsp; def&nbsp;initialize()<br class="">&nbsp; &nbsp;&nbsp;@bar&nbsp;=&nbsp;5<br class="">&nbsp;&nbsp;end</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><font face="Menlo" class="">&nbsp; def bar()</font><div class=""><font face="Menlo" class="">&nbsp; &nbsp; puts “Hello"</font></div><div class=""><font face="Menlo" class="">&nbsp; end</font></div><font face="Menlo" class="">end</font></div><div class=""><br class=""></div><div class="">In that case, how would one implement&nbsp;<font face="Menlo" class="">DynamicMemberLookupProtocol</font> for the lookup of <font face="Menlo" class="">bar</font>,&nbsp;and what would the return value be? Its entirely context sensitive.</div><div class=""><br class=""></div><div class="">If we want a model that fits the most languages, shouldn’t we redefine&nbsp;DynamicCallable to specify the function name?</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">protocol&nbsp;DynamicCallable {<br class="">&nbsp; &nbsp; associatedtype&nbsp;DynamicCallableArgument<br class="">&nbsp; &nbsp; associatedtype&nbsp;DynamicCallableResult</font><div class=""><font face="Menlo" class=""><br class=""></font></div><font face="Menlo" class="">&nbsp; &nbsp; func&nbsp;dynamicCall(function: String,&nbsp;arguments: [(String, DynamicCallableArgument)])&nbsp;throws&nbsp;-&gt; DynamicCallableResult</font><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">This would work in both languages:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">extension&nbsp;PyVal:&nbsp;DynamicMemberLookupProtocol, DynamicCallable {</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; func&nbsp;dynamicCall(function: String,&nbsp;arguments: [(String, PyVal)])&nbsp;throws -&gt;&nbsp;PyVal&nbsp;{</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; let functionVal = self[function]</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; functionVal.call(arguments)</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; }</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">extension RbVal:&nbsp;DynamicMemberLookupProtocol, DynamicCallable {</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; func&nbsp;dynamicCall(function: String,&nbsp;arguments: [(String, PyVal)])&nbsp;throws -&gt;&nbsp;PyVal&nbsp;{</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp;rb_funcallv(self,&nbsp;rb_intern(function),&nbsp;arguments.count,&nbsp;arguments);</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; }</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">Thoughts?</div><div class="">David.</div><div class=""><br class=""><blockquote type="cite" class=""><div class="">On 20 Nov 2017, at 03:16, 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="">Hi all,<br class=""><br class="">As I mentioned on a couple other threads, I’ve been working on improving Swift interoperability with Python. &nbsp;I have two pitches out: one to improve Python member lookup and one to improve calls to Python values. &nbsp;While those proposals are important, I think it is also useful to see what can be accomplished with Swift today.<br class=""><br class="">To show you how far we can get, here is a Swift playground (tested with Xcode 9) that has an example interoperability layer, and a tutorial for using it. &nbsp;If you’re interested in the pitches, or in Python, please check it out:<br class=""><br class=""><span id="cid:CE41A451-8E2D-469F-A192-A935CAC70C84@home" class="">&lt;PythonInterop.playground.zip&gt;</span><br class=""><br class="">In addition to showing how far the interop can go today (which is really quite far) it shows what the future will look like assuming the member lookup and call issues are resolved. &nbsp;To reiterate what I said on the pitch threads, I am not attached at all to the details of the two pitches themselves, I simply want the problems they address to be solved.<br class=""><br class="">Finally, of course I also welcome questions and feedback on the actual approach, naming, and other suggestions to improve the model! &nbsp;Thanks,<br class=""><br class="">-Chris<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""></div></div>_______________________________________________<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>