Polymorphic Numerics on .Net and In C#

Posted by Bent Rasmussen on Stack Overflow See other posts from Stack Overflow or by Bent Rasmussen
Published on 2010-03-08T21:05:37Z Indexed on 2010/03/08 21:06 UTC
Read the original article Hit count: 352

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET