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

Vanderlei Martinelli vmartinelli at alecrim.com
Sun Jan 31 15:08:43 CST 2016


Good idea! For the syntax I suggest something like this:

protocol MyEquatable {
    @warn_unused_result
    operator ==(lhs: Self, rhs: Self) -> Bool
}

struct MyStruct: MyEquatable {
    let foo: String
    let bar: String


    operator ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
        return lhs.foo == rhs.foo && lhs.bar == rhs.bar
    }
}


`operator` meaning `static func`


-Van


On Sun, Jan 31, 2016 at 11:14 AM, Haravikk <e-mail at haravikk.me> wrote:

> Definitely a +1 from me for the feature.
>
> What are the name lookup issues? Do you mean cases where an operator for
> Foo == Foo exists in more than one location? Personally I’d just stick with
> what we have now, i.e- treat operator implementations within a specific
> class/struct as being globally defined anyway and throw an error if the
> same signature is declared more than once.
>
> One minor issue around putting them in class/struct bodies though is that
> I wonder if perhaps a keyword other than func should be used? While they
> are functions, they aren’t methods of instances. At the very least they
> should probably need to be static.
>
> On 31 Jan 2016, at 05:26, Chris Lattner via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>
> 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
>
>
> _______________________________________________
> 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/20160131/9374f990/attachment.html>


More information about the swift-evolution mailing list