[swift-evolution] Equatability for enums with associated values

David Sweeris davesweeris at mac.com
Fri Jan 13 16:30:12 CST 2017


> On Jan 13, 2017, at 15:10, Anton Zhilin via swift-evolution <swift-evolution at swift.org> wrote:
> 
> That seems pretty close to Rust’s derive. Why not invent a similar syntax in Swift? Otherwise it will make us look through all the sources to make sure deriving is used.
> 
> enum Option: @derive Equatable {
>     ...
> }
> Also, we can get better looking compilation errors, like:
> 
> ERROR at line 1, col 14: could not derive Equatable for Option
> enum Option: @derive Equatable {
>              ^~~~~~~~~~~~~~~~~

I think that idea came up once before... can't remember where, though, or what we thought of it at the time.

As far as reducing enum boilerplate, what about borrowing the generic system's syntax and looking at it from the switch's PoV?
func == (lhs: MyEnum, rhs: MyEnum) -> Bool {
    switch <c is MyEnum> (lhs, rhs) {
    case (c(let lVal), c(let rVal)): // both lhs and rhs are "c" and the same case
        return lVal == rVal //syntax error if `==` isn't defined for the associated value types of every case
    default: return false
    }
}

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170113/7e3d21e0/attachment.html>


More information about the swift-evolution mailing list