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

Vanderlei Martinelli vmartinelli at alecrim.com
Sat Jan 30 23:03:42 CST 2016


Since the first public betas I’d like to know why operator implementation
have to be written outside the body of its owner.

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160131/32bf267e/attachment.html>


More information about the swift-evolution mailing list