10

I explored c# source code reference. And I came across with interesting mention of IArithmetic<T> interface. For example, Int32, Double contain commented implementations of IArithmetic interface. I am interested by these details. As I understood, it is attempt to add "supporting" of arithmetic operations. But why are they commented? Is it bad way to add supporting generic "operators"?

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
Alex Aparin
  • 4,393
  • 5
  • 25
  • 51
  • OP has a point - here's the commented [`IArithmetic<>`](https://referencesource.microsoft.com/#mscorlib/system/int16.cs,4f2f28ae4cd2bc79) – StuartLC Jan 24 '17 at 16:09
  • This is no longer in the coreclr [source](https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Int32.cs#L26) – DaveShaw Jan 24 '17 at 16:10
  • 4
    If you're asking us if it's bad or good or useful, it's opinion-based. If you're looking for the authoritative answer on why this was once considered and ultimately not implemented, it's [history-based](http://meta.stackoverflow.com/questions/276366/). In either case, it's not a good question for SO. – Jeroen Mostert Jan 24 '17 at 16:16
  • @JeroenMostert, thanks I understood. Voting to close question – Alex Aparin Jan 24 '17 at 16:18
  • 1
    Discussion: https://github.com/dotnet/corefx/issues/27167 – Aaron Franke Mar 16 '18 at 17:08

1 Answers1

6

It was probably scrapped due to performance reasons and not very much usability.

Primitive types supporting arithmetic operations through an interface is really not a very attractive scenario; performance would be horrible compared to simply using the value type itself due to the necessary boxing and unboxing.

What possible uses? Well, the first one to spring to mind would be the following scenario:

public Matrix<T> where T: IArithmetic<T>

or some such. Although this could be interesting, due to performance reasons, it would probably need to be solved some other way, not through interfaces; read this for very educated musing on the subject.

On top of all that, if you really need something similar to Arithmetic<T> you can always build your own with an added level of indirection.

Community
  • 1
  • 1
InBetween
  • 32,319
  • 3
  • 50
  • 90