Comparing enum cases while ignoring associated values

The use cases that use if/switch to compare to another "constant" value is not a good motivator, as you could write this today:

if case .client = foo {
}

It becomes more interesting when you need comparing two non-constant values.


This works in basic cases:

func valueBaseName(_ value: Any) -> String {
    var string = "\(value)" // string("hello")
    if let paren = string.range(of: "(") {
        string .removeSubrange(paren.lowerBound...)
        return string
    } else {
        return string
    }
}

print(valueBaseName(Foo.string("hello")))   // "string"
print(valueBaseName(Foo.number(42)))        // "number"
// used like:
valueBaseName(foo) == valueBaseName(bar)

Although it's easy to break it (e.g. by having a CustomStringConvertible implementation that does something else).


+1 to having something better built-in, ideally that's O(1) in both time and code size.