Reputation: 77641
I'd like this code to work.
I have an enum where the case Direction.Right takes a distance parameter.
enum Direction {
case Up
case Down
case Left
case Right(distance: Int)
}
Now another enum that can take a Direction parameter.
enum Blah {
case Move(direction: Direction)
}
let blah = Blah.Move(direction: Direction.Right(distance: 10))
When I switch on the Blah
enum I want to be able to conditionally switch on the Move.Right like this...
switch blah {
case .Move(let direction) where direction == .Right:
print(direction)
default:
print("")
}
But I get the error...
binary operator '==' cannot be applied to operands of type 'Direction' and '_'
Is there a way to do this?
Upvotes: 4
Views: 2647
Reputation: 539815
It is actually quite easy :)
case .Move(.Up):
print("up")
case .Move(.Right(let distance)):
print("right by", distance)
Your code
case .Move(let direction) where direction == .Right:
does not compile because ==
is defined by default only for
enumerations without associated values.
Upvotes: 8