posted by Rüdiger Klaehn on Thu 5th Aug 2004 05:00 UTC

"Generics, Page 2/2"

The problem is that the basic data types such as System.Int32 (int) and System.Double (double) do not implement such an interface, even though they support all the required arithmetic operations.

The net result is that something like the above-mentioned generic point class can not be designed to use the basic data types, which of course makes it totally useless. There exist various kludges to work around the issue. For example it would be possible to wrap the basic data types to make them support the required interface. But this requires a lot of additional code and results in unacceptably low performance.

Is there a solution?

Going back to C++ templates

First of all, there is one solution that I definitely do not want. To abolish type constraints and make .NET generics more similar to C++ templates. C++ templates are a compile-time feature much like a macro preprocessor and are thus not a good solution for a highly dynamic language such as C#.

IArithmetic<T>

The simplest practical solution would be to let the basic data types inherit an interface for arithmetic operations similar to the IComparable<T> interface. This would be a very small and unobtrusive change and offer great benefits for writing generic arithmetic types in C# and other .NET languages. No compiler or runtime changes would be nessecary. It is totally incomprehensible to me why microsoft has not done this.

Method Constraints

A more radical approach would be to support method constraints instead of just interface constraints. This would be quite complicated since you would have to specify the exact signature of the method you require. For example in the point class it is not sufficient to require that a class/struct has a + operator. You also have to specify what type the arguments and return value have. So the syntax would look somewhat like this:

struct Point<T> 
	where T: 
		T operator + (T,T),
		T operator - (T,T)
{ ... }

This is obviously much too complicated for a language like C# that strives to be simpler than C++. Method constraints would also require compiler and runtime changes, so the chances of something like this being implemented are quite low.

Implicit interface constraints

Another approach that has some of the benefits of method constraints without the arcane syntax would be implicit interface constraints. You would specify which methods you require in an interface, but classes would not have to implement this interface. They would just have to contain all the required methods with the right signatures.

struct Point<T>
	where T: implicit IArithmetic<T>
{ ... }

Here any T would be allowed that has the required methods Add, Subtract, Multiply, Divide with the right signatures. T would not have to explicitly implement the IArithmetic<T> interface, hence the name implicit interface inheritance.

This approach would IMHO be the most flexible one, but it would probably lead to a religious debate between the advocates of static and dynamic typing. It would also require changes to the compiler, so this is not likely to happen anytime soon.

The notion of an implicit interface has come up on several discussions about this topic. See for example this discussion.
I hacked a small utility class that emits automatic wrapper classes called AutoCaster. Integrating something like this into the compiler should not be too hard, so wether to support implicit interface constraints is mostly a language design issue.



Conclusion

The only solution that has a chance to make it into .NET in this decade is to add the IArithmetic<T> interface to the basic data types. But there is really no good reason not to do this as soon as possible. System.Int32, System.Single etc. already implement interfaces such as IComparable<T>, so why not IArithmetic<T>?
As I mentioned before, this would require no compiler or runtime changes. All microsoft would have to do would be to add the IArithmetic<T> interface to the System namespace and let all the basic data types explicitly implement this interface. I have no access to the source code of mscorlib.dll, but at least with mono this is trivial to do.

References

The C# language specification, Version 2.0
The proposed IArithmetic<T> interface
A generic implementation of System.Drawing.Point using IArithmetic<T>
The mono version of the System.Int32 primitive type.
The mono version of the System.Int32 primitive type, enhanced to support IArithmetic<T>

About the Author
Rüdiger Klaehn works as freelance programmer. He is currently trying to start a company with some friends, but in germany that is a long and tedious process. He is very interested in functional programming languages and hopes that .NET will lead to a more widespread adoption in the industry.
If you would like to see your thoughts or experiences with technology published, please consider writing an article for OSNews.
Table of contents
  1. "Generics, Page 1/2"
  2. "Generics, Page 2/2"
e p (0)    48 Comment(s)

Technology White Papers

See More