You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importUIKitclassGameView: UIView {}
classProblemView: UIView {}
enumGameOrProblemView {
casegame (GameView)
caseproblem (ProblemView)
// The following is ridiculous, surely there's a better way.varisGame: Bool {
ifcase .game = self { returntrue } else { returnfalse }
}
varisProblem: Bool {
ifcase .problem = self { returntrue } else { returnfalse }
}
// 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.
The text was updated successfully, but these errors were encountered:
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.
Additional Detail from JIRA
md5: 38c2b6a877daead6264c6eadd24fcb76
Issue Description:
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.
The text was updated successfully, but these errors were encountered: