<div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi Patrick,<br>FYI, we’re very likely to remove this special behavior for operators, by making operator requirements only find operators declared in a conforming type. This would eliminate the special case for operators that exists now, and has other advantages as well. Check out this proposal for more information:<br><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0091-improving-operators-in-protocols.md" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/0091-improving-operators-in-protocols.md</a></blockquote><div style="font-size:13px"><br></div><div style="font-size:13px">Thanks, hadn't seen that one yet! Looks like a good solution.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 15, 2016 at 7:10 AM, Chris Lattner <span dir="ltr"><<a href="mailto:clattner@apple.com" target="_blank">clattner@apple.com</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"><br><div><span class=""><blockquote type="cite"><div>On May 13, 2016, at 12:12 AM, Patrick Pijnappel via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div dir="ltr">For some protocols we'd like to require top-level (free) functions, e.g. for many math functions such as <font face="monospace, monospace">abs()</font> or <font face="monospace, monospace">sin()</font>. We already do this implicitly for operators. </div></div></blockquote><div><br></div></span><div>Hi Patrick,</div><div><br></div><div>FYI, we’re very likely to remove this special behavior for operators, by making operator requirements only find operators declared in a conforming type. This would eliminate the special case for operators that exists now, and has other advantages as well. Check out this proposal for more information:</div><div><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0091-improving-operators-in-protocols.md" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/0091-improving-operators-in-protocols.md</a></div><span class="HOEnZb"><font color="#888888"><div><br></div><div>-Chris</div><br></font></span><blockquote type="cite"><div><span class=""><div dir="ltr"><div><br></div><div><b>Proposal</b></div><div>Allow top-level function/property requirements in protocols, e.g.:</div><div><br></div><div><table style="border-spacing:0px;border-collapse:collapse;color:rgb(51,51,51);font-family:Helvetica,arial,nimbussansl,liberationsans,freesans,clean,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:13px"><tbody><tr><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap"><span style="color:rgb(167,29,93)">public</span> <span style="color:rgb(167,29,93)">protocol</span> <span style="color:rgb(0,134,179)">AbsoluteValuable</span> <span style="color:rgb(167,29,93)">:</span> SignedNumber {
<span style="color:rgb(150,152,150)">/// Returns the absolute value of `x`.
</span><span style="color:rgb(167,29,93)"> @warn_unused_result
</span><span style="color:rgb(167,29,93)"> toplevel</span> <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">abs</span>(<span style="color:rgb(121,93,163)">_</span> <span>x</span>: <span style="color:rgb(167,29,93)">Self</span>) <span style="color:rgb(167,29,93)">-></span> Self
}</td></tr></tbody></table><div><br></div><div>We'd probably want to require this for operators. This also opens up syntax if we ever get dynamically dispatched operators.</div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Consolas;color:rgb(0,134,179)"><span style="color:rgb(167,29,93)"><br></span></div><table style="border-spacing:0px;border-collapse:collapse;color:rgb(51,51,51);font-family:Helvetica,arial,nimbussansl,liberationsans,freesans,clean,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:13px"><tbody><tr><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap"><span style="color:rgb(167,29,93)">public</span> <span style="color:rgb(167,29,93)">protocol</span> SignedNumber <span style="color:rgb(167,29,93)">:</span> <span style="color:rgb(0,134,179)">Comparable</span>, <span style="color:rgb(0,134,179)">IntegerLiteralConvertible</span> {
<span style="color:rgb(150,152,150)">/// Returns the result of negating `x`.
</span> <span style="color:rgb(167,29,93)">@warn_unused_result
toplevel </span><span style="color:rgb(167,29,93)">prefix</span> <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">-</span> (x: <span style="color:rgb(167,29,93)">Self</span>) <span style="color:rgb(167,29,93)">-></span> Self
}</td></tr></tbody></table></div></div><div><div><br></div><div>Currently this is done using the combination of a static method and a top-level generic function on that protocol. As I understand that approach does have some benefits in terms of type-checker performance, though I'm not sure whether that is likely to stay relevant in the future.</div><div><br></div><div><b>Advantages</b></div></div><div><ul><li>Cleaner than current approach (esp. floating point types have tons of top-level functions)</li><li>Makes operators less of a special case</li><li>Opens up syntax for member operators</li><li>Could also apply to top-level properties (esp. useful if we get generic properties, for e.g. π<...>)</li></ul></div></div></span><span class="">
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></blockquote></div><br></div></blockquote></div><br></div>