I've got a trait "Value" and an extending class "Equation", like so:
trait Value {
def apply(in: Input): Int
}
class Equation ( eq: Array[Array[Value]] ) extends Value {
override def apply (in: Input) = {
eq.map(addend => addend.map( _(in) ).fold(1)(_ * _) ).fold(0)(_ + _)
}
def this(eq: String) = {
this( eq.replace("-", "+-").split('+').map( _.split('*').map(s => Value(s)) ) )
}
}
(For my purposes division isnt necessary, subtraction is solved by adding the negative version of something. I intend to remove the auxillary constructor here once I have the full string parser done, it's a quick solution that doesn't handle parentheses)
In the process of trying to parse a string into an equation, I've created an Array[Array[Equation]], because Equations inside Equations let me handle order of operations for parentheses. Since an Equation is a Value, I expected that I could pass this Array[Array[Equation]] into Equation's constructor, but then I get the following error:
overloaded method constructor Equation with alternatives:
[error] (eq: String)spekular.misc.Equation <and>
[error] (eq: Array[Array[spekular.misc.Value]])spekular.misc.Equation
[error] cannot be applied to (Array[Array[spekular.misc.Equation]])
Any idea what I'm doing wrong? I tried rewriting the constructor for an Equation (see below), but that got me more errors and seems more complex than necessary:
class Equation [T <: Value] ( eq: Array[Array[T]] ) extends Value { ... }