The mixin technique in my last post was a bit awkward to use with generic methods. It is much less awkward to use for classes, however. For example, if you want an expression evaluator much like I posted here, but you want to leave the result type open, you can use this technique to do it.

First, I declared a policy interface for the operators I wanted to abstract over,

partialinterfaceIOperatorPolicy<T> { T Add(T a, T b); T Subtract(T a, T b); T Multiply(T a, T b); T Divide(T a, T b); }

Second, I created the operator policies for

and **int**

. I could have created more, **double**```
```

, **decimal**

, etc. but
two is sufficient for demonstration.**float**

partialstructIntOperatorPolicy : IOperatorPolicy<int> {intIOperatorPolicy<int>.Add(inta,intb) {returna + b; }intIOperatorPolicy<int>.Subtract(inta,intb) {returna - b; }intIOperatorPolicy<int>.Multiply(inta,intb) {returna * b; }intIOperatorPolicy<int>.Divide(inta,intb) {returna / b; } }partial structDoubleOperatorPolicy : IOperatorPolicy<double> {doubleIOperatorPolicy<double>.Add(doublea,doubleb) {returna + b;doubleIOperatorPolicy<double>.Subtract(doublea,doubleb) {returna - b; }doubleIOperatorPolicy<double>.Multiply(doublea,doubleb) {returna * b; }doubleIOperatorPolicy<double>.Divide(doublea,doubleb) {returna / b; } }

Next are the expression nodes themselves. I wrap them in a generic class that takes the parameters. This mitigates some of the clutter caused by the policy.

partialclassExpression<T, OperatorPolicy>whereOperatorPolicy :struct, IOperatorPolicy<T> {staticOperatorPolicy _operatorPolicy =newOperatorPolicy();publicabstractpartialclassExpr {publicabstractT Evaluate(); }publicpartialclassLiteral : Expr {privateT _literal;publicLiteral(T literal) { _literal = literal; }publicoverrideT Evaluate() {return_literal; }publicoverridestringToString() {return_literal.ToString(); } }publicabstractpartialclassBinaryOp : Expr {privateExpr _left;privateExpr _right;protectedBinaryOp(Expr left, Expr right) { _left = left; _right = right; }publicoverrideT Evaluate() {returnEvaluateOp(_left.Evaluate(), _right.Evaluate()); }protectedabstractT EvaluateOp(T left, T right); }publicpartialclassAdd : BinaryOp {publicAdd(Expr left, Expr right) :base(left, right) { }protectedoverrideT EvaluateOp(T left, T right) {return_operatorPolicy.Add(left, right); } }publicpartialclassAdd : BinaryOp {publicAdd(Expr left, Expr right) :base(left, right) { }protectedoverrideT EvaluateOp(T left, T right) {return_operatorPolicy.Add(left, right); } }publicpartialclassSubtract : BinaryOp {publicSubtract(Expr left, Expr right) :base(left, right) { }protectedoverrideT EvaluateOp(T left, T right) {return_operatorPolicy.Subtract(left, right); } }publicpartialclassMultiply : BinaryOp {publicMultiply(Expr left, Expr right) :base(left, right) { }protectedoverrideT EvaluateOp(T left, T right) {return_operatorPolicy.Multiply(left, right); } }publicpartialclassDivide : BinaryOp {publicDivide(Expr left, Expr right) :base(left, right) { }protectedoverrideT EvaluateOp(T left, T right) {return_operatorPolicy.Divide(left, right); } } }

Here the `Expression`

class acts as a container of a family of
classes. Once you have an instance of one of the `Expr`

derived classes, calling
`Evaluate()`

is straight-forward. Unfortunately, constructing one is a bit of a
challenge. To get a literal of the

expression you would need to do,**int**

vari1 =newExpression<int, IntOperatorPolicy>.Literal(1);

You can make this a little better by introducing a

method into **static**`Expression`

like,

publicstaticExpr L(T literal) {returnnewLiteral(literal); }

then the above becomes,

vari1 = Expression<int, IntOperatorPolicy>.L(1);

This still a bit awkward especially when you want to construct a more complicated expression,

vare =newExpression<int, IntOperatorPolicy>.Add(i1, i1);

This can be made significantly better by adding operator overloads to `Expr`

as
in,

publicabstractpartialclassExpr {publicabstractT Evaluate();publicstaticExproperator+(Expr a, Expr b) {returnnewAdd(a, b); }publicstaticExproperator-(Expr a, Expr b) {returnnewSubtract(a, b); }publicstaticExproperator*(Expr a, Expr b) {returnnewMultiply(a, b); }publicstaticExproperator/(Expr a, Expr b) {returnnewDivide(a, b); } }

now the `e`

assignment can look like,

vare = i1 + i1;

and finally, to make it look even better you can put the code to create the
expression in a static method of a class that derives from `Expression`

and closes the
type parameters. Here are examples of one for

and
a similar one for **int**

.**double**

classTestInt : Expression<int, IntOperatorPolicy> {publicstaticvoidRun() {variv = L(1) + L(2) * L(3); Console.WriteLine(iv.Evaluate()); } }classTestDouble : Expression<double, DoubleOperatorPolicy> {publicstaticvoidRun() {vardv = L(1.2) + L(3.4) * L(5.6); Console.WriteLine(dv.Evaluate()); } }

This shows taking advantage of the C# compiler's operator overloading to
produce an expression tree and the use of the `L()`

method as a sort
of cast to change a literal of `T`

into a `Literal<T>`

.
The call to `Evaluate()`

calculates the result of the expression as
an

or **int**

.
The code generated, as discussed
last time, is quite good when JIT'ing outside the debugger and using
optimized retail bits.**double**

This use of a policy

is what I refer to
as an **struct***adapter policy*. The `Expression`

class has an
abstraction it supports, represented by `IOperatorPolicy<T>`

, but
this abstraction is not supported by any of the interesting types you would want
to pass as `Expression`

's `T`

. This is solved by creating
an *adapter policy* and passing that along with `T`

. This
allows a generic type to introduce an abstraction that is unknown to the types
the generic wants to range over, allow us, as above, to pass `int`

for `Expression`

's `T`

even though

doesn't implement **int**`IOperatorPolicy<T>`

itself.

Next time I will discuss another application of an *adapter policy*.