On Fri, 17 Apr 2009 13:25:45 +0400, Andrei Alexandrescu 
<[email protected]> wrote:

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

Yes, this also helps to catch errors at compile-time (like, dynObject.lenght vs 
dynObject.length).

Reply via email to