ADL

Tobias Müller via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 3 10:29:49 PDT 2016


Tobias M <troplin at bluewin.ch> wrote:
> On Saturday, 3 September 2016 at 16:32:16 UTC, ZombineDev wrote:
>> 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...

It's not runtime reflection, sorry about that.

But Add claims to be generic but it's actually just a list of special
cases.
It compiles for all types but only works for some. And even worse,
for types that actually do support addition but are not in the list it
silently does the wrong thing.

You cannot do the same in a truly generic way.




More information about the Digitalmars-d mailing list