ADL

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


ZombineDev <petar.p.kirov at gmail.com> wrote:
> So what? C#'s generics are less flexible than C++ and D templates.
> The point is that C#'s lookup does not consider only the 
> implemented interfaces, but also falls back to extensions 
> methods. If C# had ADL,
> the compiler would also look for extension methods in the 
> namespace
> of the type (in non-generic methods, when the type is "known"), 
> although the user of the type may not have imported the namespace.

ADL wouldn't change anything if you don't cast to a specific type, and if
you do, that part of the code is not generic anymore.

>>> 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.
> 
> There's no circumventing the type system. `typeof(obj)` is barely 
> even reflection. You can do this with regular cast or using the 
> `is` expression (http://rextester.com/CXGNK69048). I used 
> `typeof` just because it could yield better performance.

Typecasting *is* circumventing the type system.




More information about the Digitalmars-d mailing list