After reading Luca Bolognese's
blog post regarding using
C# 4.0's dynamic keyword to simulate INumeric I immediately thought that must be a way to express
this with generics instead. Generics, however, do not allow you to abstract over operators (which is what
is is needed to really do `INumeric`

) but you can simulate it using adapters as described in
this post.

Say I want to write a general average routine that is generic over what type I am using to store the data.
That is, I want one routine that works equally for

as well as **double**

.
What I want to be able to write is something like,**decimal**

publicstaticT Average(paramsT[] values) { T total = 0;foreach(varvinvalues) total = total + v;returntotal / values.Length; }

where the `T`

is any type that supports adding and dividing. I can't get this code to
compile exactly but we can get surprisingly close! My final version looks like,

classMath<T, A> : PolicyUser<T, A>whereA :struct, IOperatorPolicy<T> {publicstaticT Average(paramsT[] values) {vartotal = L(0);foreach(varvinvalues) total = total + v;returntotal / values.Length; } }

which is much closer to the original than I thought originally could get!

I started by cloning the operator policy and added a `FromInt()`

I will explain below,

interfaceIOperatorPolicy<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); T FromInt(intvalue); }

Then I cloned the

policy struct and then added a **double**

mainly by cut/paste/search/replace**decimal**

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; }doubleIOperatorPolicy<double>.FromInt(intvalue) {returnvalue; } }structDecimalOperatorPolicy : IOperatorPolicy<decimal> {decimalIOperatorPolicy<decimal>.Add(decimala,decimalb) {returna + b; }decimalIOperatorPolicy<decimal>.Subtract(decimala,decimalb) {returna - b; }decimalIOperatorPolicy<decimal>.Multiply(decimala,decimalb) {returna * b; }decimalIOperatorPolicy<decimal>.Divide(decimala,decimalb) {returna / b; }decimalIOperatorPolicy<decimal>.FromInt(int value) {returnvalue; } }

The cleaver bit is to define a base type that contains a `struct`

that defines the operators
as overloads. The overloads use a policy

to implement that overloaded operators. This **struct**```
```

also defines implicit conversion operators to hide some (but not all) of the messiness introduced by using this
wrapper type. The **struct**`PolicyUser`

class looks like,

classPolicyUser<T, A>whereA:struct, IOperatorPolicy<T> {staticA Policy =newA();protectedstructValue {publicT V;publicValue(T v) { V = v; }publicstaticValueoperator+(Value a, Value b) {returnnewValue(Policy.Add(a.V, b.V)); }publicstaticValueoperator-(Value a, Value b) {returnnewValue(Policy.Subtract(a.V, b.V)); }publicstaticValueoperator*(Value a, Value b) {returnnewValue(Policy.Multiply(a.V, b.V)); }publicstaticValueoperator/(Value a, Value b) {returnnewValue(Policy.Divide(a.V, b.V)); }publicstaticimplicitoperatorValue(T v) {returnnewValue(v); }publicstaticimplicitoperatorValue(intv) {returnnewValue(Policy.FromInt(v)); }publicstaticimplicitoperatorT(Value v) {returnv.V; } }protectedstaticValue L(intvalue) {returnnewValue(Policy.FromInt(value)); } }

What the `Value`

struct does is allows you to use the type `Value`

in place of the type `T`

whenever you want to
use operators. When operators are used you only need to ensure one sub-expression is promoted to `Value`

and the implict operator will
take care of the rest.

One remaining oddness is using literals. Implicit operators have there limits which makes using literal a bit strange. This was also odd in the
Policy post as well and I used the same trick to make it a little less cumbersome. I introduced
a static method `L()`

that takes an integer and converts it to `Value`

allowing you to use integer literals by calling `FromInt()`

I introduced earlier. You can
extend to this to allow other types by repeating the pattern for, say

, allowing you to use **double**

literals as well. I didn't
because I didn't need it for my example (but probably will if I try to implement something more complicated.
**double**

To bind the actual data type to provide the data type and the policy. To make this simpler I created two create implementations of `Math`

.

classDoubleMath : Math<double, DoubleOperatorPolicy> { }classDecimalMath : Math<decimal, DecimalOperatorPolicy> { }

Calling the average method looks like,

varresultDouble = DoubleMath.Average(1, 2, 3, 4, 5);varresultDecimal = DecimalMath.Average(1, 2, 3, 4, 5);

It should be straight forward to see how this could be adapted to a `Financial`

class, as Luca was trying to build, and how the routines could
be made independent of the data type used in the calculations.

There you have it. Policies can be used to simulate `INumeric`

without having to resort to using

.**dynamic**

Edit: Mar 9, 2009: Fixed HTML formatting error