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-10930] Test for enum doesn't yield a Bool #53321

Open
swift-ci opened this issue Jun 14, 2019 · 2 comments
Open

[SR-10930] Test for enum doesn't yield a Bool #53321

swift-ci opened this issue Jun 14, 2019 · 2 comments
Labels
compiler The Swift compiler in itself feature A feature request or implementation improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10930
Radar None
Original Reporter smartgo (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, LanguageFeatureRequest
Assignee None
Priority Medium

md5: 38c2b6a877daead6264c6eadd24fcb76

Issue Description:

import UIKit


class GameView: UIView {}
class ProblemView: UIView {}


enum GameOrProblemView {
    case game (GameView)
    case problem (ProblemView)


    // The following is ridiculous, surely there's a better way.
    var isGame: Bool {
        if case .game = self { return true } else { return false }
    }
    var isProblem: Bool {
        if case .problem = self { return true } else { return false }
    }


    // Ideally, the following should work:
    // var isGame1: Bool { return self == .game }
    // Error: Binary operator '==' cannot be applied to operands of type 'GameOrProblemView' and '_'


    // Since it works in an 'if', I'd expect the case expression to be a Bool,
    // but this doesn't work either:
    // var isGame2: Bool { return (case .game = self) }
    // Error: Cannot convert return expression of type '()' to return type 'Bool'
}

See the playground above: trying to simply return a Bool depending on the enum. The obvious 'return self == .game' doesn't work. After finding the convoluted 'if case .game = self' expression, it's natural to expect that one to yield a Bool that can be returned, but not so.

Maybe I'm missing something obvious? It seems to me that enums are not working as they should in this case – testing for one of the cases should be super simple.

@belkadan
Copy link
Contributor

Yeah, case is not an expression right now; if case is essentially shorthand for a switch with a single case. Part of the reason this is "still" not in the language is because there are two ideas for how to fix it: figure out how to make a case-like expression (without breaking current case syntax in switches), or generate implicit members on an enum for testing, like isGame.

@belkadan
Copy link
Contributor

Either approach would then go through the Swift Evolution Process.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 feature A feature request or implementation improvement
Projects
None yet
Development

No branches or pull requests

2 participants