Fully dynamic d by opDotExp overloading

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Apr 17 15:16:57 PDT 2009


Christopher Wright wrote:
> Andrei Alexandrescu wrote:
>> Yah, glad someone mentioned it :o). The best way is a blend - you can 
>> statically dispatch on some popular/heavily-used names, then rely on a 
>> hashtable lookup for dynamic stuff.
>>
>> Andrei
> 
> 
> You suggest:
> 
> auto opDotExp(string name)(...)
> {
>     static if (name == "something")
>     {
>         code...
>     }
>     else
>     {
>         dynamic stuff
>     }
> }
> 
> That isn't very clear. Why not write it this way:
> 
> auto opDotExp(string name, ...)
> {
>     dynamic stuff
> }
> 
> auto something (...)
> {
>     code...
> }

It's a good question. opDotExp leaves more flexibility because it allows 
for a host of compile-time manipulations, e.g. decide to forward to a 
member etc. Also consider this (probably Nick will turn blue):

struct Pascalize(T)
{
     T m;
     auto opDotExp(string name, T...)(T args)
     {
         return mixin("m."~tolower(name))(args);
     }
}

struct S { void foo() { ... } }

Pascalize!S s;
s.foo(); // works
s.Foo(); // works too
s.fOo(); // yup, works again


Andrei



More information about the Digitalmars-d mailing list