[swift-evolution] Operator implementation inside struct/class body

Chris Lattner clattner at apple.com
Sat Jan 30 23:26:46 CST 2016


> On Jan 30, 2016, at 9:03 PM, Vanderlei Martinelli via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Since the first public betas I’d like to know why operator implementation have to be written outside the body of its owner.

Yep, this is a generally desirable feature (at least for symmetric operators).  This would also be great to get dynamic dispatch of operators within class declarations.  I don’t think we have a firm proposal nailing down how name lookup works with this though.

-Chris

> 
> Take as example the code:
> 
> protocol MyEquatable {
>     @warn_unused_result
>     func ==(lhs: Self, rhs: Self) -> Bool
> }
> 
> struct MyStruct: MyEquatable {
>     let foo: String
>     let bar: String
> }
> 
> func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
>     return lhs.foo == rhs.foo && lhs.bar == rhs.bar
> }
> 
> Why we cannot write:
> 
> protocol MyEquatable {
>     @warn_unused_result
>     func ==(lhs: Self, rhs: Self) -> Bool
> }
> 
> struct MyStruct: MyEquatable {
>     let foo: String
>     let bar: String
>     
>     func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
>         return lhs.foo == rhs.foo && lhs.bar == rhs.bar
>     }
> 
> }
> 
> Any thoughts?
> 
> 
> -Van
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160130/7f26f5d3/attachment.html>


More information about the swift-evolution mailing list