Polymorphic Numerics on .Net and In C#
- by Bent Rasmussen
It's a real shame that in .Net there is no polymorphism for numbers, i.e. no INumeric interface that unifies the different kinds of numerical types such as bool, byte, uint, int, etc. In the extreme one would like a complete package of abstract algebra types.
Joe Duffy has an article about the issue:
http://www.bluebytesoftware.com/blog/CommentView,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx
How would you express this in C#, in order to retrofit it, without having influence over .Net or C#?
I have one idea that involves first defining one or more abstract types (interfaces such as INumeric - or more abstract than that) and then defining structs that implement these and wrap types such as int while providing operations that return the new type (e.g. Integer32 : INumeric; where addition would be defined as
public Integer32 Add(Integer32 other)
{
return Return(Value + other.Value);
}
I am somewhat afraid of the execution speed of this code but at least it is abstract.
No operator overloading goodness...
Any other ideas?
.Net doesn't look like a viable long-term platform if it cannot have this kind of abstraction I think - and be efficient about it.
Abstraction is reuse.