Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-2900] binary operator == cannot be applied to two MyEnum operands #45494

Closed
mattneub opened this issue Oct 9, 2016 · 3 comments
Closed
Labels
compiler The Swift compiler in itself conformances Feature → protocol: protocol conformances derived conformances Feature → protocol → conformances: derived conformances aka synthesized conformances enum Feature → type declarations: Swift enumeration declarations Equatable Area → standard library: The `Equatable` protocol feature A feature request or implementation swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented swift 3.0 type checker Area → compiler: Semantic analysis

Comments

@mattneub
Copy link

mattneub commented Oct 9, 2016

Previous ID SR-2900
Radar None
Original Reporter @mattneub
Type Bug
Environment

Xcode 8. Swift 3

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 4da3829a65b5e5dc537fe5c501b942aa

Issue Description:

enum WeatherType {
    case cloudy(coverage: Int)
    case sunny
    case windy
}
let w = WeatherType.sunny
if w == .sunny {} // error
if WeatherType.sunny == w {} // error

This seems unnecessarily severe. Use of == with enum instances is a natural convenience; in fact, it's kind of the point of enums. Telling me I can't do it just because some case has an associated value is a bit rough.

@belkadan
Copy link
Contributor

I think this just falls under the general "the compiler should be able to synthesize Equatable for you when all the types involved are themselves Equatable". (You do still need to declare it, though.)

What Swift will not do is "partial equality" where you can use == with some cases but not others. Doing that kind of detection statically is not worth contorting the language around, especially when there's if case .sunny = w.

@mattneub
Copy link
Author

Another reason why this situation is surprising is that it works for Optional. I appreciate that this is because support for use of `==` with Optional operands is explicitly built into the language; but to the naive user it feels as if this means that equality comparison should "just work" with an enum even if the enum has a case with an associated value.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added improvement new feature and removed bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. improvement labels Jun 2, 2022
@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation type checker Area → compiler: Semantic analysis conformances Feature → protocol: protocol conformances derived conformances Feature → protocol → conformances: derived conformances aka synthesized conformances enum Feature → type declarations: Swift enumeration declarations Equatable Area → standard library: The `Equatable` protocol swift 3.0 swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented and removed new feature labels Feb 28, 2023
@AnthonyLatsis
Copy link
Collaborator

This is SE-0185.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself conformances Feature → protocol: protocol conformances derived conformances Feature → protocol → conformances: derived conformances aka synthesized conformances enum Feature → type declarations: Swift enumeration declarations Equatable Area → standard library: The `Equatable` protocol feature A feature request or implementation swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented swift 3.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants