Reputation: 633
I want to evaluate expressions like:
LARGEST(A) + B
A is double[] {1,2,3,4,5}
B is double[] {2,3,4,5,6}
I'm using Irony and can create a suitable abstract syntax tree:
1. binary_add
2. function_largest
3. var_A
4. var_B
The question is what sort of class or variable is flexible enough to house different types of intermediate results in the calculation process?
The intermediate results can be either scalar or vector (i.e. double versus double[]) and can't be known until evaluation time.
The code needs to figure out which Add overload to use at runtime based on the previous intermediate result:
void Add(double[] vector, double scalar) {}
void Add(double scalar1, double scalar2) {}
void Add(double[] vector1, double[] vector2) {}
evaluation order
binary_add 4. {7,8,9,10,11} vector (calculated element-wise addition of largest()'s scalar result to each vector entry)
function_largest 2. 5 scalar (calculated)
var_A 1. {1,2,3,4,5} vector (given)
var_B 3. {2,3,4,5,6} vector (given)
Last resort I can just use object
and do a lot of .GetType()
in an if-statement to pick a suitable overload, but is there a smarter or more idiomatic way that I'm not thinking of? (Dynamics? Generics? etc.)
Upvotes: 0
Views: 26