I'm making a very simple calculator and I'm getting a really strange compile time error. I'm getting the following error in my CalculatorBrain
class:
Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
Here is the code that generated the error
private var operations: Dictionary<String, Operation> = [
"π" : .Constant(M_PI),
"±" : .UnaryOperation({ -$0 }),
"×" : .BinaryOperation({ $0 * $1 }),
"÷" : .BinaryOperation({ $0 / $1 }),
"+" : .BinaryOperation({ $0 + $1 }),
"−" : .BinaryOperation({ $0 - $1 }),
"=" : .Equals
]
The strange thing is that if I remove the following:
"±" : .UnaryOperation({ -$0 })
"+" : .BinaryOperation({ $0 + $1 })
"−" : .BinaryOperation({ $0 - $1 })
The code compiles, otherwise it throws the error.
Another strange thing is that if I change those to:
"±" : .UnaryOperation({ (op1: Double) -> Double in return -op1 })
"+" : .BinaryOperation({ (op1: Double, op2: Double) -> Double in return op1 + op2 })
"−" : .BinaryOperation({ (op1: Double, op2: Double) -> Double in return op1 - op2 })
The code compiles and does not throw the error.
I'm kind of confused as to why it works when using the operators the *
and /
and not -
and +
Just in case you're wondering how Operation
is implemented, here it is:
private enum Operation {
case Constant(Double)
case UnaryOperation((Double) -> Double)
case BinaryOperation((Double, Double) -> Double)
case Equals
}
I'm using Swift version 2.2 on Xcode Version 7.3.1