1

Is there some interface like this in .Net

public interface IOperable<T>
{

    T Add(T add);
    T Substract(T subs);
    T Multiply(T mult);
    T Divide(T div);
    //and so on
}

For manage generics that you know will be operable? Or even primitives.

For example for a class Interval<T> we will be able to get the Lenght if End and Start will be IOperable (End.Substract(Start);).

I know ISet<T> operations, but that approach is for non continuous cualitative elements, but for continuous elements i didn't see any approach in c#.

If not, why not exists in current .Net? Developing reasons? Philosophical? Are there any library that offers this approach?

DrkDeveloper
  • 939
  • 7
  • 17

1 Answers1

2

"Nope".

If it ever happens, it almost certainly won't be via an interface, but: via generic operator constraint support.

There are some semantic issues with an interface: not all number systems implement all the same operators - and the results aren't always the same (division and multiplication of vectors / matrices, for example; or: you can add and subtract two TimeSpans, but you can't multiply or divide them, etc); and even when they do: the meaning can change (integer division is not the same as floating point division, and subtraction and negation get weird for unsigned data types, for example).

Generic operator constraints are something that have been discussed a few times, but have never made it into the language.

You can cheat with dynamic, but that involves "boxing", so has some non-trivial overhead. Or there are (or were) some generic operator services built into MiscUtil.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • And why not accept some "IllegalOperationException" depending of the number system?. for example if we Multiply two `TimeSpan`s it will throw an `IllegalOperationException`. Isn't good approach? – DrkDeveloper Apr 12 '18 at 08:28
  • "via generic operator constraint support.". Can you give an example? – DrkDeveloper Apr 12 '18 at 08:31
  • @DrkDeveloper for an example: not really, because it *hasn't ever made it into the language*, but I can *invent* something: `void SomeMethod(...) where T : operator T +(T, T), operator T -(T, T)` - as for `IllegalOperationException` - that means you're not *really* implementing the interface - you're just *pretending* to; which tends to be a *really bad way of doing interfaces* – Marc Gravell Apr 12 '18 at 08:36
  • I'll go with the "dynamic" approach for my case, Thanks. And thanks for the "the interfaces should be fully implemented" vision, i didn't see it that way until now. You learnt me today a new thing. – DrkDeveloper Apr 12 '18 at 08:42
  • @DrkDeveloper note also: generic operator support would also require new IL opcodes, and JIT and CLI changes (to actually apply the right operations based on the `T`, and to handle "lifted" operators), and framework changes to reflection-invoke; so: I wouldn't hold your breath. It would need a very good use-case as to why it is suddenly essential when it clearly hasn't been essential (no matter how "nice to have") in all the rest of .NET's history – Marc Gravell Apr 12 '18 at 08:48