On Saturday, 3 September 2016 at 16:32:16 UTC, ZombineDev wrote:
No you're wrong. There's no need for interfaces or for generic constraints. It's not static vs duck typing. It's just a method lookup issue. See for yourself: http://rextester.com/GFKNSK99121

Ok, Interfaces and other generic methods with compatible constraints. But in the end you cannot do much without any interface constraints except writing out to the console as you do in the example.

But the main point still holds, name lookup is only done at definition time, not at instantiation time. That's why you can only call generic methods. Overloads don't work.

Sum is implemented in that stupid way, because unlike C++, in C# operators need to be implemented as static methods, so you can't abstract them with an interface. If they were instance methods, you could implement them outside of the class as extension methods and there would be no need to write a distinct method for each type. Here's an example: http://rextester.com/PQFPC46087 The only thing missing is syntax sugar to forward the '+' operator to 'Add' in my example.

With runtime reflection you can do almost anything... That's circumventing the type system and doesn't disprove anything. I mean, it even "works" for types that cannot be added at all, by just returning a default value...
      • Re: ADL Walter Bright via Digitalmars-d
      • Re: ADL Timon Gehr via Digitalmars-d
      • Re: ADL Walter Bright via Digitalmars-d
      • Re: ADL Jacob Carlborg via Digitalmars-d
      • Re: ADL Manu via Digitalmars-d
      • Re: ADL Walter Bright via Digitalmars-d
      • Re: ADL Tobias M via Digitalmars-d
      • Re: ADL ZombineDev via Digitalmars-d
      • Re: ADL Tobias M via Digitalmars-d
      • Re: ADL ZombineDev via Digitalmars-d
      • Re: ADL Tobias M via Digitalmars-d
      • Re: ADL Tobias Müller via Digitalmars-d
      • Re: ADL ZombineDev via Digitalmars-d
      • Re: ADL Tobias Müller via Digitalmars-d
      • Re: ADL ZombineDev via Digitalmars-d
      • Re: ADL Walter Bright via Digitalmars-d
      • Re: ADL Andrei Alexandrescu via Digitalmars-d
  • Re: ADL Timon Gehr via Digitalmars-d
    • Re: ADL Walter Bright via Digitalmars-d
      • Re: ADL Walter Bright via Digitalmars-d
    • Re: ADL Stefan Koch via Digitalmars-d

Reply via email to