<div dir="ltr"><div>If the argument symmetry should be preserved, I think I'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) -> Self<br> public static func addWithOverflow(lhs: Self, _ rhs: Self) -> (Self, overflow: Bool)<br>}<br><br>public func +<T: IntegerArithmeticType>(lhs: T, rhs: T) -> T {<br> return T.add(lhs, rhs)<br>}<br>public func &+<T: IntegerArithmeticType>(lhs: T, rhs: T) -> T {<br> return T.addWithOverflow(lhs, rhs).0<br>}<br><br><br>public protocol Equatable {<br> public static func equal(lhs: Self, _ rhs: Self) -> Bool<br>}<br><br>public func ==<T: Equatable>(lhs: T, rhs: T) -> T {<br> return T.equal(lhs, rhs)<br>}<br>public func !=<T: Equatable>(lhs: T, rhs: T) -> 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"><<a href="mailto:tseitz42@icloud.com" target="_blank">tseitz42@icloud.com</a>></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't think symmetric operators should be mapped to asymmetric methods which are only dispatched on the receiver'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'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 <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>:<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>) -> <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>) -> (<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">) -> </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>) -> (<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 +<T: IntegerArithmeticType>(lhs: T, rhs: T) -> T {</div><div><span style="white-space:pre-wrap">        </span>return lhs.add(hrs)</div><div>}</div><div><br></div><div><div>public func &+<T: IntegerArithmeticType>(lhs: T, rhs: T) -> 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>) -> <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>) -> <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 ==<T: Equatable>(lhs: T, rhs: T) -> T {</div><div><span style="white-space:pre-wrap">        </span>return lhs.equals(hrs)</div><div>}</div></div><div><div><div>public func !=<T: Equatable>(lhs: T, rhs: T) -> 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>
> Hello.<br>> <br>> The proposal can be also read <a>athttps://gist.github.com/vmartinelli/67d6ad234c7a4e14f8d5</a><br>> <br>> 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>> <br>> Opinions, comments and corrections (including on English grammar) are all welcome. :-)<br>> <br>> -Van<br>> <br>> ---------<br>> <br>> Instance Operators<br>> 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>> Author:Vanderlei Martinelli(<a href="https://github.com/vmartinelli" target="_blank">https://github.com/vmartinelli</a>)<br>> Status:Awaiting review<br>> Review manager: TBD<br>> <br>> Introduction<br>> <br>> The proposal aims to move operator implementation from the global and static scope into extension/struct/class instance scope.<br>> <br>> <br>> 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>> <br>> Motivation<br>> <br>> 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>> <br>> <br>> Example:<br>> <br>> // MARK: - protocolpublicprotocolMyDoubleType {publicfuncsomeUsefulFunction()publicfunc*(lhs:Self, rhs:Self)->Selfpublicpostfixfunc++(inoutx:Self)->Self }// MARK: - implementationextensionDouble: MyDoubleType {publicfuncsomeUsefulFunction() {// ...}// we cannot implement the operators here...}// ... but have to implement them herepublicfunc*(lhs:Double, rhs:Double)->Double{returnlhs.multipliedBy(rhs) }publicpostfixfunc++(inoutx:Double)->Double{<span> </span> x+=1.0returnx }<br>> <br>> <br>> 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>> <br>> Proposed solution<br>> <br>> Move the operator implementation into the extension/struct/class scope and turn operator funcs into instance funcs, using theoperatorkeyword.<br>> <br>> Detailed design<br>> Protocol conformance<br>> <br>> After the change the above code can be written like the example bellow.<br>> <br>> // MARK: - protocolpublicprotocolMyDoubleType {publicfuncsomeUsefulFunction()publicoperator*(rhs:Self)->Selfpublicmutatingpostfixoperator++()->Self}// MARK: - implementationextensionDouble: MyDoubleType {publicfuncsomeUsefulFunction() {// ...}publicoperator*(rhs:Double)->Double{returnself.multipliedBy(rhs)<span> </span> }publicmutatingpostfixoperator++()->Double{self+=1.0returnself} }<br>> <br>> Operator funcs everywhere<br>> <br>> 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>> <br>> Operator internal names<br>> <br>> Perhaps because of the internal implementation of Swift, operators have to have names to be handled. The suggestion is to adopt__operator__GreaterThanOrEqualfor a>=operator, as example. The operator introduction would be:<br>> <br>> infixoperator>={associativitynoneprecedence130name"GreaterThanOrEqual"}<br>> <br>> <br>> So the code will be written like this...<br>> <br>> structMyStruct {operator>=(other: MyStruct)->Bool{return...} }<br>> <br>> <br>> ... but translated internally to this:<br>> <br>> structMyStruct {func__operator__GreaterThanOrEqual(other: MyStruct)->Bool{return...} }<br>> <br>> Impact on existing code<br>> <br>> 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>> <br>> <br>> Also thefunckeyword would be deprecated for operators as well, using theoperatorto declare/implement an operator func.<br>> <br>> Alternatives considered<br>> Status quo<br>> <br>> Leave things as they are. Even being inconsistent or not allowing new possibilities that instance operators will bring.<br>> <br>> Static implementation inside extension/struct/class scope<br>> <br>> 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>> <br>> <br>> 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>> <br>> <br>> Example:<br>> <br>> // MARK: - protocolpublicprotocolMyDoubleType {publicfuncsomeUsefulFunction()publicstaticoperator*(lhs:Self, rhs:Self)->Selfpublicstaticoperator/(lhs:Int64, rhs:Int64)->Int64// what?publicstaticpostfixoperator++(inoutx:Self)->Self}// MARK: - implementationextensionDouble: MyDoubleType {publicfuncsomeUsefulFunction() {// ...}publicstaticoperator*(lhs:Double, rhs:Double)->Double{returnlhs.multipliedBy(rhs)<span> </span> }// this should be implemented inside a Int64 type, not here...publicstaticoperator/(lhs:Int64, rhs:Int64)->Int64{// ...}publicstaticpostfixoperator++(inoutx:Double)->Double{<span> </span> <span> </span> x+=1.0returnx<span> </span> } }<br>> <br>> <br>> <br>><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>