Fully dynamic d by opDotExp overloading

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Apr 17 15:09:35 PDT 2009


Christopher Wright wrote:
> Andrei Alexandrescu wrote:
>> Cool! I suggest the rewrite:
>>
>> c.unknownmethod(args) -> c.opDotExp!("unknownmethod")(args)
>>
>> That way you have the option of handling the method name statically or 
>> dynamically.
> 
> How would that allow you to handle the method name dynamically, if 
> you're passing it as a template argument?
> 
> You mean that the *callee* can be dynamic. However, the *caller* cannot. 

Of course. It makes no sense to ask for integrated syntax with a 
variable string. Think of it for a minute.

> This would rarely be an issue, I grant, but:
> 
> Let's say you have a set of valid arguments for the opDotExp template. 
> Why the hell aren't you writing individual methods?!
> 
> So opDotExp is nearly useless if you make the method name a template 
> argument. The *only* uses are
> - blacklisting arguments, with compile-time errors
> - requiring that arguments follow a certain (regular) pattern, with 
> compile-time errors
> - a way to get the __FUNCTION__ macro that's been requested several 
> times and not yet implemented

I don't think I quite understand. Let me repeat: passing the string as a 
template gives you static+dynamic. Passing the string as a runtime value 
gives you dynamic. To clarify:

class Dynamo
{
     Variant call(string name, Variant[] args...)
     {
         ...
     }
     Variant opDotExp(string name, T...)(T args)
     {
         return call(name, variantArray(args));
     }
     ...
}

Now you can say:

Dynamo d;
d.foo();
string bar = chomp(readln);
d.call(bar);

Makes sense?

Andrei



More information about the Digitalmars-d mailing list