Fully dynamic d by opDotExp overloading

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Apr 17 02:25:45 PDT 2009


davidl wrote:
> After tweaking dmd a bit litte, i get the dotexp overloading work.
> 
> The following is the test code:
> import std.stdio;
> class c
> {
> 
>     B opDotExp(char[] methodname,...)
>     {
>         writefln("god it works ", methodname);
>       return new B();
>     }
>     void opAdd(int j)
>     {
> 
>     }
>     void test()
>     {
>     }
> }
> 
> class a:c
> {
> 
> }
> 
> class B
> {
>   int i;
>   B opAssign(int k){
>     i=k;
>     return this;
>   }
> }
> 
> char[] v1;
> 
> void func(char[] v, ...){}
> 
> void main()
> {
>    a v=new a;
>    v.test();
>    v.dynamicmethod(3,4);
>    //v.qq = 5;
>    writefln((v.qq = 5).i);
> }
> 
> it generates the output:
> god it works dynamicmethod
> god it works qq
> 5
> 
> Any comments? Do you like this feature?
> 

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.


Andrei



More information about the Digitalmars-d mailing list