<div dir="ltr">HI, this proposal looks really interesting!<div><br></div><div>I have a few questions:</div><div><br></div><div><b><font size="4">Clarity on the proposal's intent</font></b></div><div><b>Nice cheap bridges, or lowering barriers to bridging?</b></div><div><br></div><div>I can see this providing a nice quick interface to Python from Swift, but I'm not sure if the exposed interface will be very Swifty (you probably have a much better idea of what is Swifty ;) than I do though). It seems you want it to be possible for everything to be dynamically exposed, I've used similar with Lua's meta methods, and I found it to be very powerful, you could basically implement inheritance in the language, which wasn't necessarily a good thing in retrospect.</div><div><br></div>Is it common for the the argument labels in other languages to be open ended, or are labels typically finite? If the answer is finite, why not use a Swift method as the wrapper?<div>Do you want duck typing, and would it be better to expose this via a protocol?</div><div><br></div><div><div>It seems like in almost every case you could do something like this:</div><div><br></div><div><p style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="color:rgb(186,45,162)">func</span> myMethod<X: <span style="text-decoration:underline;color:rgb(112,61,170)">P</span><span style="color:rgb(112,61,170)">ythonConvertible</span> & <span style="color:rgb(112,61,170)">CanQuack</span>, Y: <span style="color:rgb(112,61,170)">PythonConvertible</span>>(a: <span style="color:rgb(79,129,135)">X</span>? = <span style="color:rgb(186,45,162)">nil</span>, b: <span style="color:rgb(79,129,135)">Y</span>) {</p><p style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"> <span style="text-decoration:underline">p</span>ythonBridge.call(<span style="color:rgb(209,47,27)">"myMethod"</span>, arguments: [<span style="color:rgb(209,47,27)">"a"</span>: <span style="color:rgb(79,129,135)">X</span>, <span style="color:rgb(209,47,27)">"b"</span>: <span style="color:rgb(79,129,135)">Y</span>])</p><p style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">}</p></div></div><div><br></div><div>It might be good to add some <b>use-cases</b> (a popular Python library perhaps) to the proposal where this type of bridge would be insufficient :).</div><div><br></div><div>It seems like this proposal pushes the responsibility of Swifty-ness and type-safety to the caller. At some point you'll have to write a type-safe bridging layer, or write your entire program in non-Swifty code ("The most obvious way to write code should also behave in a safe manner"). Is the main goal to <b>lower the barrier to Python</b> and other dynamic languages? or is it to provide a cheap nice Swifty bridge? I have the above concerns about the latter.</div><div><br></div><div><b><font size="4">Alternative sugar</font></b></div><div><br></div><div>Ruby has Keyword Arguments for similar sugar:</div><div><font face="monospace, monospace"><b><br></b></font></div><div><font face="monospace, monospace"><b>def</b> foo(regular, hash={})</font><br></div><div><font face="monospace, monospace"> <b>puts</b> "the hash: #{</font><span style="font-family:monospace,monospace">hash}</span><span style="font-family:monospace,monospace">"</span></div><div><br></div><div><br></div><div>I'm sure you're aware of it, but I'll explain for completeness, any trailing argument labels are stored and passed as a hash:</div><div><br></div><div><pre class="gmail-lang-rb gmail-prettyprint gmail-prettyprinted" style="margin-top:0px;margin-bottom:1em;padding:5px;border:0px;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-stretch:inherit;line-height:inherit;vertical-align:baseline;width:auto;max-height:600px;overflow:auto;color:rgb(57,51,24);word-wrap:normal"><span class="gmail-pln" style="font-family:inherit;font-style:inherit;font-variant-caps:inherit;white-space:inherit;margin:0px;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;color:rgb(48,51,54)">foo</span><span class="gmail-pun" style="font-family:inherit;font-style:inherit;font-variant-caps:inherit;white-space:inherit;margin:0px;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;color:rgb(48,51,54)">(</span><span class="gmail-pln" style="font-family:inherit;font-style:inherit;font-variant-caps:inherit;white-space:inherit;margin:0px;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;color:rgb(48,51,54)">regular</span><span class="gmail-pun" style="font-family:inherit;font-style:inherit;font-variant-caps:inherit;white-space:inherit;margin:0px;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;color:rgb(48,51,54)">,</span><span class="gmail-pln" style="font-family:inherit;font-style:inherit;font-variant-caps:inherit;white-space:inherit;margin:0px;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;color:rgb(48,51,54)"> bar: "hello", bas: 123</span><span class="gmail-pun" style="font-family:inherit;font-style:inherit;font-variant-caps:inherit;white-space:inherit;margin:0px;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;color:rgb(48,51,54)">) <b># outputs 'the hash: [</b></span><b><span style="color:rgb(48,51,54);font-family:inherit;font-style:inherit;font-variant-caps:inherit;white-space:inherit">bar: "hello", bas: 123</span><span style="color:rgb(48,51,54);font-family:inherit;font-style:inherit;font-variant-caps:inherit;white-space:inherit">]'</span></b></pre></div><div>Have you considered an alternative like this? For example:</div><div><br></div><div><p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(186,45,162)">func</span><span style="color:rgb(0,0,0)"> myMethod(regular: </span>Int<span style="color:rgb(0,0,0)">, store: </span><span style="color:rgb(186,45,162)">@argcapture</span><span style="color:rgb(0,0,0)"> [</span>String<span style="color:rgb(0,0,0)">: </span>PythonConvertible<span style="color:rgb(0,0,0)">]) -> </span>PythonConvertible</p></div><div><br></div><div>I'm sure you have good reasons, it might make the implementation bleed out into other parts of the codebase. It would be good to include it in the proposal <b>alternatives</b> section though. At the moment most of the "alternatives" in the proposal just seem to be extensions to the same solution :)</div><div><br></div><div><b><font size="4">Clarity</font></b></div><div><b>Perhaps just that things are more clear to me now</b></div><div><br></div><div>If my extrapolation is correct a user will implement a single type that will allow a subset of a good portion of another language to be exposed (object method and property bridging). I'm guessing that the dynamic member proposal you're planning will not work with methods, it will require a property, I think this helps explain some of the motivations. It might be nice to have a more complete example that includes dynamic members. I didn't find it clear from the proposal that it would only be necessary to implement this protocol once per language.</div><div><br></div><div>Thanks for considering my questions,</div><div>Andrew Bennett</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Nov 11, 2017 at 4:37 AM, Chris Lattner via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hello all,</div><div><br></div><div>I have a couple of proposals cooking in a quest to make Swift interoperate with dynamically typed languages like Python better. Instead of baking in hard coded support for one language or the other, I’m preferring to add a few small but general purpose capabilities to Swift. This is the first, which allows a Swift type to become “callable”.</div><div><br></div><div>The proposal is here:</div><div><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d" target="_blank">https://gist.github.com/<wbr>lattner/<wbr>a6257f425f55fe39fd6ac7a2354d69<wbr>3d</a></div><div><br></div><div>I’ve also attached a snapshot below, but it will drift out of date as the proposal is refined. Feedback and thoughts are appreciated, thanks!</div><div><br></div><div>-Chris</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><h1 style="box-sizing:border-box;margin-right:0px;margin-bottom:16px;margin-left:0px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal;margin-top:0px!important">Introduce user-defined dynamically "callable" types</h1><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><li style="box-sizing:border-box">Proposal: <a href="https://gist.github.com/lattner/NNNN-DynamicCallable.md" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none" target="_blank">SE-NNNN</a></li><li style="box-sizing:border-box;margin-top:0.25em">Author: <a href="https://github.com/lattner" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none" target="_blank">Chris Lattner</a></li><li style="box-sizing:border-box;margin-top:0.25em">Review Manager: TBD</li><li style="box-sizing:border-box;margin-top:0.25em">Status: <span style="box-sizing:border-box;font-weight:600">Awaiting implementation</span></li></ul><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#introduction" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-introduction" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Introduction</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">This proposal introduces a new <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">DynamicCallable</code> protocol to the standard library. Types that conform to it are "callable" with the function call syntax. It is simple syntactic sugar which allows the user to write:</p><div class="m_7488414618697653947highlight m_7488414618697653947highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",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-radius:3px;word-break:normal"> a <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">someValue</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">keyword1</span>: <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">42</span>, <span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>foo<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>, <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">keyword2</span>: <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">19</span>)</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">and have it be interpreted by the compiler as:</p><div class="m_7488414618697653947highlight m_7488414618697653947highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",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-radius:3px;word-break:normal"> a <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> someValue.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">dynamicCall</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">argument<wbr>s</span>: [
(<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>keyword1<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>, <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">42</span>), (<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>, <span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>foo<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>), (<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>keyword2<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>, <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">19</span>)
])</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">Other languages have analogous features (e.g. Python "callables"), but the primary motivation of this proposal is to allow elegant and natural interoperation with dynamic languages in Swift.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">Swift-evolution thread: <a href="https://lists.swift.org/pipermail/swift-evolution/" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none" target="_blank">Discussion thread topic for that proposal</a></p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#motivation" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-motivation" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Motivation</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">Swift is well known for being exceptional at interworking with existing C and Objective-C APIs, but its support for calling APIs written in scripting langauges like Python, Perl, and Ruby is quite lacking. These languages provide an extremely dynamic programming model where almost everything is discovered at runtime.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">Through the introduction of this proposal, and the related <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">DynamicMemberLookupPro<wbr>tocol</code> proposal, we seek to fix this problem. We believe we can make many common APIs feel very natural to use directly from Swift without all the complexity of implementing something like the Clang importer. For example, consider this Python code:</p><div class="m_7488414618697653947highlight m_7488414618697653947highlight-source-python" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",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-radius:3px;word-break:normal"><span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">class</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">Dog</span>:
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">def</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">__init__</span>(<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box">self</span>, <span class="m_7488414618697653947pl-smi" style="box-sizing:border-box">name</span>):
<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">self</span>.name <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> name
<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">self</span>.tricks <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> [] <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">#</span> creates a new empty list for each dog</span>
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">def</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">add_trick</span>(<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box">self</span>, <span class="m_7488414618697653947pl-smi" style="box-sizing:border-box">trick</span>):
<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">self</span>.tricks.append(trick)</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">we would like to be able to use this from Swift like this (the comments show the corresponding syntax you would use in Python):</p><div class="m_7488414618697653947highlight m_7488414618697653947highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",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-radius:3px;word-break:normal"> <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> import DogModule</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> import DogModule.Dog as Dog // an alternate</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> Dog <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> Python.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">import</span>(“DogModule.<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box">Dog</span><span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>)<span class="m_7488414618697653947pl-ii" style="box-sizing:border-box;color:rgb(250,251,252);background-color:rgb(179,29,40)"></span></span>
<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-ii" style="box-sizing:border-box;color:rgb(250,251,252);background-color:rgb(179,29,40)"></span></span>
<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"> // dog = Dog(<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>Brianna<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>)<span class="m_7488414618697653947pl-ii" style="box-sizing:border-box;color:rgb(250,251,252);background-color:rgb(179,29,40)"></span></span>
<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"> let dog = Dog(<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>Brianna<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>)<span class="m_7488414618697653947pl-ii" style="box-sizing:border-box;color:rgb(250,251,252);background-color:rgb(179,29,40)"></span></span>
<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-ii" style="box-sizing:border-box;color:rgb(250,251,252);background-color:rgb(179,29,40)"></span></span>
<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"> // dog.add_trick(<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>Roll over<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>)<span class="m_7488414618697653947pl-ii" style="box-sizing:border-box;color:rgb(250,251,252);background-color:rgb(179,29,40)"></span></span>
<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"> dog.add_trick(<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>Roll over<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>)<span class="m_7488414618697653947pl-ii" style="box-sizing:border-box;color:rgb(250,251,252);background-color:rgb(179,29,40)"></span></span>
<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-ii" style="box-sizing:border-box;color:rgb(250,251,252);background-color:rgb(179,29,40)"></span></span>
<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"> // dog2 = Dog(<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>Kaylee<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>).add_trick(<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>snore<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box"><wbr>"</span>)<span class="m_7488414618697653947pl-ii" style="box-sizing:border-box;color:rgb(250,251,252);background-color:rgb(179,29,40)"></span></span>
<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"> let dog2 = Dog(<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>Kaylee<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>).add_trick(<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>snore<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box"><wbr>"</span>)<span class="m_7488414618697653947pl-ii" style="box-sizing:border-box;color:rgb(250,251,252);background-color:rgb(179,29,40)"></span></span></pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">Of course, this would also apply to standard Python APIs as well. Here is an example working with the Python <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">pickle</code>API and the builtin Python function <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">open</code>:</p><div class="m_7488414618697653947highlight m_7488414618697653947highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",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-radius:3px;word-break:normal"> <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> import pickle</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> pickle <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> Python.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">import</span>(<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>pickle<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>)
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> file = open(filename)</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> file <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> Python.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">open</span>(filename)
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> blob = file.read()</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> blob <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> file.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">read</span>()
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> result = pickle.loads(blob)</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> result <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> pickle.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">loads</span>(blob)</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">This can all be expressed today as library functionality written in Swift, but without this proposal, the code required is unnecessarily verbose and gross. Without it (but with the related dynamic member lookup proposal) the code would have a method name (like <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">call</code>) all over the code:</p><div class="m_7488414618697653947highlight m_7488414618697653947highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",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-radius:3px;word-break:normal"> <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> import pickle</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> pickle <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> Python.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">import</span>(<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>pickle<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>) <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> normal method in Swift, no change.</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> file = open(filename)</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> file <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> Python.<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box">open</span>.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">call</span>(filename)
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> blob = file.read()</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> blob <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> file.<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box">read</span>.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">call</span>()
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> result = pickle.loads(blob)</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> result <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> pickle.<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box">loads</span>.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">call</span>(blob)
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> dog2 = Dog("Kaylee").add_trick("<wbr>snore")</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> dog2 <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> Dog.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">call</span>(<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>Kaylee<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>).<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box">add_trick</span>.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">c<wbr>all</span>(<span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>snore<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>)</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">While this is a syntactic sugar proposal, we believe that this expands Swift to be usable in important new domains. This sort of capability is also highly precedented in other languages, and is a generally useful language feature that could be used for other purposes as well.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#proposed-solution" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-proposed-solution" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Proposed solution</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">We propose introducing this protocol to the standard library:</p><div class="m_7488414618697653947highlight m_7488414618697653947highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",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-radius:3px;word-break:normal"><span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">protocol</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">DynamicCallable</span> {
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">associatedtype</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">DynamicCallableArgument</span>
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">associatedtype</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">DynamicCallableResult</span>
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">dynamicCall</span>(<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box"><span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">arguments</span></span>: [(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">String</span>, DynamicCallableArgument)]) <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">throws</span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-></span> DynamicCallableResult
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">It also extends the language such that function call syntax - when applied to a value of <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">DynamicCallable</code> type - is accepted and transformed into a call to the <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">dynamicCall</code> member. The <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">dynamicCall</code> method takes a list of tuples: the first element is the keyword label (or an empty string if absent) and the second value is the formal parameter specified at the call site.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">Before this proposal, the Swift language has two types that participate in call syntax: functions and metatypes (for initialization). Neither of those may conform to protocols at the moment, so this introduces no possible ambiguity into the language.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">It is worth noting that this does not introduce the ability to provide dynamicly callable <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">static</code>/<code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">class</code> members. We don't believe that this is important given the goal of supporting dynamic languages like Python, but if there is a usecase discovered in the future, it could be explored as future work. Such future work should keep in mind that call syntax on metatypes is already meaningful, and that ambiguity would have to be resolved somehow.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#discussion" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-discussion" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Discussion</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">While the signature for <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">dynamicCall</code> is highly general we expect the most common use will be clients who are programming against concrete types that implement this proposal. One very nice aspect of this is that, as a result of Swift's existing subtyping mechanics, implementations of this type can choose whether they can actually throw an error or not. For example, consider this silly implementation:</p><div class="m_7488414618697653947highlight m_7488414618697653947highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",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-radius:3px;word-break:normal"><span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">struct</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">ParameterSummer</span> : <span class="m_7488414618697653947pl-e" style="box-sizing:border-box;color:rgb(111,66,193)">DynamicCallable </span>{
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">dynamicCall</span>(<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box"><span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">arguments</span></span>: [(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">String</span>, <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Int</span>)]) <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-></span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Int</span> {
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">return</span> arguments.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">reduce</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">0</span>) { <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">$0</span><span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">+</span><span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">$1</span>.1 }
}
}
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> x <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">ParameterSummer</span>()
<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">print</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">x</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">1</span>, <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">7</span>, <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">12</span>)) <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> prints 20</span></pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">Because <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">ParameterSummer</code>'s implementation of <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">dynamicCall</code> does not throw, the call site is known not to throw either, so the print doesn't need to be marked with <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">try</code>.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#example-usage" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-example-usage" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Example Usage</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">A more realistic (and motivating) example comes from a prototype Python interop layer. While the concrete details of this use case are subject to change and not important for this proposal, it is perhaps useful to have a concrete example to see how this comes together.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">That prototype currently has two types which model Python values, one of which handles Python exceptions and one of which does not. Their conformances would look like this, enabling the use cases described in the Motivation section above:</p><div class="m_7488414618697653947highlight m_7488414618697653947highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",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-radius:3px;word-break:normal"><span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">extension</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">ThrowingPyRef</span>: <span class="m_7488414618697653947pl-e" style="box-sizing:border-box;color:rgb(111,66,193)">DynamicCallable </span>{
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">dynamicCall</span>(<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box"><span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">arguments</span></span>: [(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">String</span>, PythonConvertible)]) <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">throws</span>
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-></span> PythonConvertible {
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> Make sure state errors are not around.</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">assert</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">PyErr_Occurred</span>() <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">==</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">nil</span>, <span class="m_7488414618697653947pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span>Python threw an error but wasn't handled<span class="m_7488414618697653947pl-pds" style="box-sizing:border-box">"</span></span>)
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> Count how many keyword arguments are in the list.</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> numKeywords <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> arguments.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">reduce</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">0</span>) {
<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">$0</span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">+</span> (<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">$1</span>.0.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">isEmpty</span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">?</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">0</span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">:</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">1</span>)
}
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> kwdict <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> numKeywords <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">!=</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">0</span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">?</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">PyDict_New</span>() <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">:</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">nil</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> Non-keyword arguments are passed as a tuple of values.</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> argTuple <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">PyTuple_New</span>(arguments.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">count</span><span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-</span>nu<wbr>mKeywords)<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">!</span>
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">var</span> nonKeywordIndex <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">0</span>
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">for</span> (keyword, argValue) <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">in</span> arguments {
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">if</span> keyword.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">isEmpty</span> {
<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">PyTuple_SetItem</span>(argTuple, nonKeywordIndex, argValue.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">toPython</span>())
nonKeywordIndex <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">+=</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">1</span>
} <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">else</span> {
<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">PyDict_SetItem</span>(kwdict<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">!</span>, keyword.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">toPython</span>(), argValue.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">toPython</span>())
}
}
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> Python calls always return a non-null value when successful. If the</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> Python function produces the equivalent of C "void", it returns the None</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> value. A null result of PyObjectCall happens when there is an error,</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> like 'self' not being a Python callable.</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">guard</span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> resultPtr <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">PyObject_Call</span>(state, argTuple, kwdict) <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">else</span> {
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">throw</span> PythonError.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">invalidCall</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">self</span>)
}
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> result <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">PyRef</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">owned</span>: resultPtr)
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> Translate a Python exception into a Swift error if one was thrown.</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">if</span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> exception <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">PyErr_Occurred</span>() {
<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">PyErr_Clear</span>()
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">throw</span> PythonError.<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">exception</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">PyRef</span>(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">bo<wbr>rrowed</span>: exception))
}
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">return</span> result
}
}
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">extension</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">PyRef</span>: <span class="m_7488414618697653947pl-e" style="box-sizing:border-box;color:rgb(111,66,193)">DynamicCallable </span>{
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">dynamicCall</span>(<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box"><span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">arguments</span></span>: [(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">String</span>, PythonConvertible)])
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-></span> PythonConvertible {
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> Same as above, but internally aborts instead of throwing Swift</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">//</span> errors.</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> }
}</pre></div><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#source-compatibility" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-source-compatibility" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Source compatibility</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">This is a strictly additive proposal with no source breaking changes.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#effect-on-abi-stability" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-effect-on-abi-stability" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Effect on ABI stability</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">This is a strictly additive proposal with no ABI breaking changes.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#effect-on-api-resilience" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-effect-on-api-resilience" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Effect on API resilience</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">This has no impact on API resilience which is not already captured by other language features.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#alternatives-considered" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-alternatives-considered" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Alternatives considered</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">A few alternatives were considered:</p><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#add-ability-to-reject-parameter-labels" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-add-ability-to-reject-parameter-labels" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Add ability to reject parameter labels</h3><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">The implementation above does not allow an implementation to staticly reject argument labels. If this was important to add, we could add another protocol to model this, along the lines of:</p><div class="m_7488414618697653947highlight m_7488414618697653947highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",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-radius:3px;word-break:normal"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">///</span> A type conforming just to this protocol would not accept parameter</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">///</span> labels in its calls.</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span><span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">protocol</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">DynamicCallable</span> {
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">associatedtype</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">DynamicCallableArgument</span>
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">associatedtype</span> <span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">DynamicCallableResult</span>
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">dynamicCall</span>(<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box"><span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">arguments</span></span>: [DynamicCallableArgument]) <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">throws</span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-></span> DynamicCallableResult
}
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">///</span> A type conforming to this protocol does allow optional parameter</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="m_7488414618697653947pl-c" style="box-sizing:border-box">///</span> labels.</span>
<span class="m_7488414618697653947pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span><span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">protocol</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">DynamicCallableWithKeywordsToo</span> : <span class="m_7488414618697653947pl-e" style="box-sizing:border-box;color:rgb(111,66,193)">DynamicCallable </span>{
<span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">dynamicCall</span>(<span class="m_7488414618697653947pl-smi" style="box-sizing:border-box"><span class="m_7488414618697653947pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">arguments</span></span>: [(<span class="m_7488414618697653947pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">String</span>, DynamicCallableArgument)]) <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">throws</span> <span class="m_7488414618697653947pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-></span> DynamicCallableResult
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">This would allow a type to implement one or the other based on their capabilities. This proposal is going with a very simple design, but if there is demand for this, the author is happy to switch.</p><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#staticly-checking-for-exact-signatures" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-staticly-checking-for-exact-signatures" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Staticly checking for exact signatures</h3><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">This protocol does not allow a type to specify an exact signature for the callable - a specific number of parameters with specific types. If we went down that route, the best approach would be to introduce a new declaration kind (which would end up being very similar to get-only subscripts) since, in general, a type could want multiple concrete callable signatures, and those signatures should participate in overload resolution.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">While such a feature could be interesting for some use cases, it is almost entirely orthogonal from this proposal: it addresses different use cases and does not solve the needs of this proposal. It does not address our needs because even a variadic callable declaration would not provide access to the keyword argument labels we need.</p><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#direct-language-support-for-python" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-direct-language-support-for-python" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Direct language support for Python</h3><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">We considered implementing something analogous to the Clang importer for Python, which would add a first class Python specific type(s) to Swift language or standard library. We rejected this option because it would be significantly more invasive in the compiler, would set the precedent for all other dynamic languages to get first class language support, and because that first class support doesn't substantially improve the experience of working with Python over existing Swift with a couple small "generally useful" extensions like this one.</p><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-variant-ligatures:normal"><a href="https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d#naming" class="m_7488414618697653947anchor" id="m_7488414618697653947user-content-naming" style="box-sizing:border-box;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Naming</h3><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">The most fertile ground for bikeshedding is the naming of the protocol and the members. We welcome other ideas and suggestions for naming, but here are some thoughts on obvious options to consider:</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">We considered but rejected the name <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">CustomCallable</code>, because the existing <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">Custom*</code> protocols in the standard library (<code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">CustomStringConvertible</code>, <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">Cust<wbr>omReflectable</code>, etc) provide a way to override and custom existing builtin abilities of Swift. In contrast, this feature grants a new capability to a type.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal">We considered but rejected a name like <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">ExpressibleByCalling</code> to fit with the <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">ExpressibleBy*</code> family of protocols (like <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">ExpressibleByFloatLitera<wbr>l</code>, <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">ExpressibleByStringLiteral</code>, etc). This name family is specifically used by literal syntax, and calls are not literals. Additionally the type itself is not "expressible by calling" - instead, instances of the type may be called.</p><div style="box-sizing:border-box;margin-top:0px;color:rgb(36,41,46);font-family:-apple-system,system-ui,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;font-variant-ligatures:normal;margin-bottom:0px!important">On member and associated type naming, we intentionally gave these long and verbose names so they stay out of the way of user code completion. The members of this protocol are really just compiler interoperability glue. If there was a Swift attribute to disable the members from showing up in code completion, we would use it (such an attribute would also be useful for the <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.05);border-radius:3px">LiteralConvertible</code> and other compiler magic protocols).</div></div><div><br></div><div><br></div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>