<div dir="ltr"><div>If the argument symmetry should be preserved, I think I&#39;d still prefer to leave operators out of this, and consistently use static methods, like this:<br><br>public protocol IntegerArithmeticType {<br>    public static func add(lhs: Self, _ rhs: Self) -&gt; Self<br>    public static func addWithOverflow(lhs: Self, _ rhs: Self) -&gt; (Self, overflow: Bool)<br>}<br><br>public func +&lt;T: IntegerArithmeticType&gt;(lhs: T, rhs: T) -&gt; T {<br>    return T.add(lhs, rhs)<br>}<br>public func &amp;+&lt;T: IntegerArithmeticType&gt;(lhs: T, rhs: T) -&gt; T {<br>    return T.addWithOverflow(lhs, rhs).0<br>}<br><br><br>public protocol Equatable {<br>    public static func equal(lhs: Self, _ rhs: Self) -&gt; Bool<br>}<br><br>public func ==&lt;T: Equatable&gt;(lhs: T, rhs: T) -&gt; T {<br>    return T.equal(lhs, rhs)<br>}<br>public func !=&lt;T: Equatable&gt;(lhs: T, rhs: T) -&gt; T {<br>    return !T.equals(lhs, rhs)<br>}<br><br><br>--<br></div>Nicola<br><div><br><div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 26, 2016 at 4:18 PM, Thorsten Seitz <span dir="ltr">&lt;<a href="mailto:tseitz42@icloud.com" target="_blank">tseitz42@icloud.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div></div><div>I don&#39;t think symmetric operators should be mapped to asymmetric methods which are only dispatched on the receiver&#39;s type.</div><div><br></div><div>Of course the current scheme using global functions with no dynamic dispatch at all is not much better (the selected function depends on the static types I happen to see at the call site but at least it isn&#39;t preferring one of the arguments.</div><div>Having real multiple dispatch would be nice :-)</div><div><br></div><div>-Thorsten </div><div><div class="h5"><div><br>Am 26.02.2016 um 11:41 schrieb Nicola Salmoria via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;:<br><br></div><blockquote type="cite"><div><div>I’m wondering if we might be trying to solve the wrong problem here.</div><div><br></div><div>Let’s look for example at part of the definition of the IntegerArithmeticType protocol (disregarding implementation details):</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(187,44,162)">public</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">protocol</span><span style="color:rgb(0,0,0)"> IntegerArithmeticType</span><span style="color:rgb(0,0,0)"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">func</span> +(lhs: <span style="color:rgb(112,61,170)">Self</span>, rhs: <span style="color:rgb(112,61,170)">Self</span>) -&gt; <span style="color:rgb(112,61,170)">Self</span></div></div><div><span style="color:rgb(112,61,170)"><br></span></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">static</span> <span style="color:rgb(187,44,162)">func</span> addWithOverflow(lhs: <span style="color:rgb(112,61,170)">Self</span>, <span style="color:rgb(187,44,162)">_</span> rhs: <span style="color:rgb(112,61,170)">Self</span>) -&gt; (<span style="color:rgb(112,61,170)">Self</span>, overflow: <span style="color:rgb(112,61,170)">Bool</span>)</div></div><div>}</div><div><br></div><div>There’s an obvious asymmetry here: + is defined as “public func”, while addWithOverflow is “public static func”.</div><div><br></div><div>Perhaps instead of extending how operators are defined, it would be more efficient to reconsider how protocol conformance should be defined.</div><div>It seems to me that a ‘swifter’ way to do the above would be</div><div><br></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(187,44,162)">public</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">protocol</span><span style="color:rgb(0,0,0)"> IntegerArithmeticType</span><span style="color:rgb(0,0,0)"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><br></div><div style="margin:0px;line-height:normal"><font face="Menlo" color="#008400"><span style="font-size:11px">    </span></font><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px">public</span><font face="Menlo" color="#008400"><span style="font-size:11px"> </span></font><font face="Menlo" color="#bb2ca2"><span style="font-size:11px">func</span></font><font face="Menlo" color="#008400"><span style="font-size:11px"> add(to: </span></font><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">Self</span><font face="Menlo" color="#008400"><span style="font-size:11px">) -&gt; </span></font><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">Self</span></div></div><div><span style="color:rgb(112,61,170)"><br></span></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">func</span> addWithOverflow(to: <span style="color:rgb(112,61,170)">Self</span>) -&gt; (<span style="color:rgb(112,61,170)">Self</span>, overflow: <span style="color:rgb(112,61,170)">Bool</span>)</div></div><div>}</div></div><div><br></div><div>and then generic operators can simply be defined once at global scope:</div><div><br></div><div>public func +&lt;T: IntegerArithmeticType&gt;(lhs: T, rhs: T) -&gt; T {</div><div><span style="white-space:pre-wrap">        </span>return lhs.add(hrs)</div><div>}</div><div><br></div><div><div>public func &amp;+&lt;T: IntegerArithmeticType&gt;(lhs: T, rhs: T) -&gt; T {</div><div><span style="white-space:pre-wrap">        </span>return lhs.addWithOverflow(hrs).0</div><div>}</div></div><div><br></div><div><br></div><div>Equatable and Comparable have similar asymmetries:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">/// Instances of conforming types can be compared for value equality</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">/// using operators `==` and `!=`.</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">///</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">/// When adopting `Equatable`, only the `==` operator is required to be</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">/// implemented.  The standard library provides an implementation for `!=`.</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">public<span style="color:rgb(0,0,0)"> </span>protocol<span style="color:rgb(0,0,0)"> Equatable {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">func</span> ==(lhs: <span style="color:rgb(112,61,170)">Self</span>, rhs: <span style="color:rgb(112,61,170)">Self</span>) -&gt; <span style="color:rgb(112,61,170)">Bool</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div></div><div><br></div><div>so we need to define ==, but not != because it’s derived from the former.</div><div>Again, the cause of the inconsistency is the requirement to define the operator as part of the protocol conformance. If it was done like this, it might seem simpler and more symmetrical:</div><div><br></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">public<span style="color:rgb(0,0,0)"> </span>protocol<span style="color:rgb(0,0,0)"> Equatable {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">func</span> equals(other: <span style="color:rgb(112,61,170)">Self</span>) -&gt; <span style="color:rgb(112,61,170)">Bool</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div></div></div><div><br></div><div><div>public func ==&lt;T: Equatable&gt;(lhs: T, rhs: T) -&gt; T {</div><div><span style="white-space:pre-wrap">        </span>return lhs.equals(hrs)</div><div>}</div></div><div><div><div>public func !=&lt;T: Equatable&gt;(lhs: T, rhs: T) -&gt; T {</div><div><span style="white-space:pre-wrap">        </span>return !lhs.equals(hrs)</div><div>}</div></div></div><div><br></div><div>Nicola</div><div><br></div>
&gt; Hello.<br>&gt; <br>&gt; The proposal can be also read <a>athttps://gist.github.com/vmartinelli/67d6ad234c7a4e14f8d5</a><br>&gt; <br>&gt; Original thread:https://<a href="http://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/008508.html" target="_blank">lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/008508.html</a><br>&gt; <br>&gt; Opinions, comments and corrections (including on English grammar) are all welcome. :-)<br>&gt; <br>&gt; -Van<br>&gt; <br>&gt; ---------<br>&gt; <br>&gt; Instance Operators<br>&gt; Proposal:SE-NNNN(<a href="https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-instance-operators.md" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-instance-operators.md</a>)<br>&gt; Author:Vanderlei Martinelli(<a href="https://github.com/vmartinelli" target="_blank">https://github.com/vmartinelli</a>)<br>&gt; Status:Awaiting review<br>&gt; Review manager: TBD<br>&gt; <br>&gt; Introduction<br>&gt; <br>&gt; The proposal aims to move operator implementation from the global and static scope into extension/struct/class instance scope.<br>&gt; <br>&gt; <br>&gt; Swift-evolution thread:link to the discussion thread for that proposal(<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/008508.html" target="_blank">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/008508.html</a>)<br>&gt; <br>&gt; Motivation<br>&gt; <br>&gt; When writing the protocol interface the operator is declarated inside the scope of that protocol, but its implementation has to be static and global. This, besides being inconsistent, might not the behaviour expected by programmers coming from other languages that have some kind of support for interface/protocol and operator implementation.<br>&gt; <br>&gt; <br>&gt; Example:<br>&gt; <br>&gt; // MARK: - protocolpublicprotocolMyDoubleType {publicfuncsomeUsefulFunction()publicfunc*(lhs:Self, rhs:Self)-&gt;Selfpublicpostfixfunc++(inoutx:Self)-&gt;Self }// MARK: - implementationextensionDouble: MyDoubleType {publicfuncsomeUsefulFunction() {// ...}// we cannot implement the operators here...}// ... but have to implement them herepublicfunc*(lhs:Double, rhs:Double)-&gt;Double{returnlhs.multipliedBy(rhs) }publicpostfixfunc++(inoutx:Double)-&gt;Double{<span>  </span> x+=1.0returnx }<br>&gt; <br>&gt; <br>&gt; Also the current implementation does not leave much room for future expansion in the use of operators (such as conversion between values, for example).<br>&gt; <br>&gt; Proposed solution<br>&gt; <br>&gt; Move the operator implementation into the extension/struct/class scope and turn operator funcs into instance funcs, using theoperatorkeyword.<br>&gt; <br>&gt; Detailed design<br>&gt; Protocol conformance<br>&gt; <br>&gt; After the change the above code can be written like the example bellow.<br>&gt; <br>&gt; // MARK: - protocolpublicprotocolMyDoubleType {publicfuncsomeUsefulFunction()publicoperator*(rhs:Self)-&gt;Selfpublicmutatingpostfixoperator++()-&gt;Self}// MARK: - implementationextensionDouble: MyDoubleType {publicfuncsomeUsefulFunction() {// ...}publicoperator*(rhs:Double)-&gt;Double{returnself.multipliedBy(rhs)<span>  </span> }publicmutatingpostfixoperator++()-&gt;Double{self+=1.0returnself} }<br>&gt; <br>&gt; Operator funcs everywhere<br>&gt; <br>&gt; An operator does not have to be implemented only to conform to a protocol, however. It can be also be implemented in any other place where a common func is. This means that even the current form can be supported.<br>&gt; <br>&gt; Operator internal names<br>&gt; <br>&gt; Perhaps because of the internal implementation of Swift, operators have to have names to be handled. The suggestion is to adopt__operator__GreaterThanOrEqualfor a&gt;=operator, as example. The operator introduction would be:<br>&gt; <br>&gt; infixoperator&gt;={associativitynoneprecedence130name&quot;GreaterThanOrEqual&quot;}<br>&gt; <br>&gt; <br>&gt; So the code will be written like this...<br>&gt; <br>&gt; structMyStruct {operator&gt;=(other: MyStruct)-&gt;Bool{return...} }<br>&gt; <br>&gt; <br>&gt; ... but translated internally to this:<br>&gt; <br>&gt; structMyStruct {func__operator__GreaterThanOrEqual(other: MyStruct)-&gt;Bool{return...} }<br>&gt; <br>&gt; Impact on existing code<br>&gt; <br>&gt; Since after this change an operator can be implemented in any other place where a common func can be, the current implementation may continue to exist, but marked as deprecated with a compiler/analyser warning.<br>&gt; <br>&gt; <br>&gt; Also thefunckeyword would be deprecated for operators as well, using theoperatorto declare/implement an operator func.<br>&gt; <br>&gt; Alternatives considered<br>&gt; Status quo<br>&gt; <br>&gt; Leave things as they are. Even being inconsistent or not allowing new possibilities that instance operators will bring.<br>&gt; <br>&gt; Static implementation inside extension/struct/class scope<br>&gt; <br>&gt; This is the way operators are implemented in C#, for example. The change would be only aesthetic. The functionality would remain the same as today.<br>&gt; <br>&gt; <br>&gt; As the types may differ from protocol/structure/class, this would allow state within the scope of operators that have nothing to do with that type. Not a good thing. In this case it might be better to keep things as they are.<br>&gt; <br>&gt; <br>&gt; Example:<br>&gt; <br>&gt; // MARK: - protocolpublicprotocolMyDoubleType {publicfuncsomeUsefulFunction()publicstaticoperator*(lhs:Self, rhs:Self)-&gt;Selfpublicstaticoperator/(lhs:Int64, rhs:Int64)-&gt;Int64// what?publicstaticpostfixoperator++(inoutx:Self)-&gt;Self}// MARK: - implementationextensionDouble: MyDoubleType {publicfuncsomeUsefulFunction() {// ...}publicstaticoperator*(lhs:Double, rhs:Double)-&gt;Double{returnlhs.multipliedBy(rhs)<span>  </span> }// this should be implemented inside a Int64 type, not here...publicstaticoperator/(lhs:Int64, rhs:Int64)-&gt;Int64{// ...}publicstaticpostfixoperator++(inoutx:Double)-&gt;Double{<span> </span> <span>  </span> x+=1.0returnx<span>  </span> } }<br>&gt; <br>&gt; <br>&gt; <br>&gt;<span> </span>

</div></blockquote></div></div><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></blockquote></div><br></div></div></div></div>