<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class="">On Jan 30, 2016, at 11:03 PM, Vanderlei Martinelli via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></blockquote><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Since the first public betas I’d like to know why operator implementation have to be written outside the body of its owner.<div class=""><br class=""></div><div class="">Take as example the code:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""><span style="color:rgb(116,137,31)" class="">protocol</span> MyEquatable {</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(116, 137, 31);" class=""><span style="color:rgb(80,111,116)" class=""> </span>@warn_unused_result</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""> <span style="color:rgb(116,137,31)" class="">func</span> ==(lhs: <span style="color:rgb(215,96,27)" class="">Self</span>, rhs: <span style="color:rgb(215,96,27)" class="">Self</span>) -> <span style="color:rgb(251,153,3)" class="">Bool</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116); min-height: 17px;" class=""><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""><span style="color:rgb(116,137,31)" class="">struct</span> MyStruct: <span style="color:rgb(18,101,182)" class="">MyEquatable</span> {</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""> <span style="color:rgb(116,137,31)" class="">let</span> foo: <span style="color:rgb(251,153,3)" class="">String</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""> <span style="color:rgb(116,137,31)" class="">let</span> bar: <span style="color:rgb(251,153,3)" class="">String</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116); min-height: 17px;" class=""><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""><span style="color:rgb(116,137,31)" class="">func</span> ==(lhs: <span style="color:rgb(255,147,0)" class="">MyStruct</span>, rhs: <span style="color:rgb(255,147,0)" class="">MyStruct</span>) -> <span style="color:rgb(251,153,3)" class="">Bool</span> {</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""> <span style="color:rgb(116,137,31)" class="">return</span> lhs.<span style="color:rgb(14,123,195)" class="">foo</span> == rhs.<span style="color:rgb(14,123,195)" class="">foo</span> && lhs.<span style="color:rgb(14,123,195)" class="">bar</span> == rhs.<span style="color:rgb(14,123,195)" class="">bar</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116); min-height: 17px;" class=""><br class=""></div></div><div class="">Why we cannot write:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""><span style="color:rgb(116,137,31)" class="">protocol</span> MyEquatable {</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(116, 137, 31);" class=""><span style="color:rgb(80,111,116)" class=""> </span>@warn_unused_result</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""> <span style="color:rgb(116,137,31)" class="">func</span> ==(lhs: <span style="color:rgb(215,96,27)" class="">Self</span>, rhs: <span style="color:rgb(215,96,27)" class="">Self</span>) -> <span style="color:rgb(251,153,3)" class="">Bool</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116); min-height: 17px;" class=""><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""><span style="color:rgb(116,137,31)" class="">struct</span> MyStruct: <span style="color:rgb(18,101,182)" class="">MyEquatable</span> {</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""> <span style="color:rgb(116,137,31)" class="">let</span> foo: <span style="color:rgb(251,153,3)" class="">String</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""> <span style="color:rgb(116,137,31)" class="">let</span> bar: <span style="color:rgb(251,153,3)" class="">String</span></div><p style="margin:0px;font-size:12px;line-height:normal;font-family:'Input Mono';color:rgb(80,111,116);min-height:17px" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""> <span style="color:rgb(116,137,31)" class="">func</span> ==(lhs: <span style="color:rgb(255,147,0)" class="">MyStruct</span>, rhs: <span style="color:rgb(255,147,0)" class="">MyStruct</span>) -> Bool {</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""> <span style="color:rgb(116,137,31)" class="">return</span> lhs.foo == rhs.foo && lhs.bar == rhs.bar</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class=""> }</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116); min-height: 17px;" class=""><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Input Mono'; color: rgb(80, 111, 116); min-height: 17px;" class=""><br class=""></div></div><div class="">Any thoughts?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">-Van</div></div></div></blockquote><br class=""></div>+1. I’ve been meaning to write up something about this for some time. The thing I’d change is to include only the right-hand side as an argument, and use ‘self’ to refer to the left-hand side:<div class=""><br class=""></div><div class=""><div class="">struct MyStruct: MyEquatable {</div><div class=""> let foo: String</div><div class=""> let bar: String</div><div class=""> </div><div class=""> func ==(other: MyStruct) -> Bool {</div><div class=""> return self.foo == other.foo && self.bar == other.bar</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">Charles</div><div class=""><br class=""></div></div></body></html>