I just started to learn Swift and am trying to figure out pattern matching.
I found the following example:
private enum Entities{
case Operand(Double)
case UnaryOperation(Double -> Double)
case BinaryOperation((Double, Double) -> Double)
}
and then I use pattern matching to figure out the Entity type
func evaluate(entity: Entities) -> Double? {
switch entity{
case .Operand(let operand):
return operand;
case .UnaryOperation(let operation):
return operation(prevExtractedOperand1);
case .BynaryOperation(let operation):
return operation(prevExtractedOperand1, prevExtractedOperand2);
}
}
The syntax for getting the bound value seems a little strange, but it works great.
After that, I found that pattern matching could be used in the expression if, so I tried to do the same withif
if case entity = .Operand(let operand){
return operand
}
but the compiler throws an error. The expected delimiter ",", which I suspect has nothing to do with the real cause of the error.
Could you help me understand what is wrong with my attempt to use pattern matching in an expression if?