Fully dynamic d by opDotExp overloading

davidl davidl at nospam.org
Fri Apr 17 07:59:20 PDT 2009


在 Fri, 17 Apr 2009 21:44:09 +0800,Leandro Lucarella <llucax at gmail.com>  
写道:

> davidl, el 17 de abril a las 14:31 me escribiste:
>> 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?
>
> This is awsome indeed. I'd love to see it in the specs. The suggestion of
> making opDotExp a template it's good one too. I guess that now that opDot
> is replaced by alias this, opDot can be used for this instead of  
> opDotExp.
>
> I don't fully understand the example though. In writefln((v.qq = 5).i),
> how is that B.i is assigned to 5 if the opDotExp("qq", 5) don't propagate
> the 5 to the new B()?
>
> Thanks for the great job.
>

Thanks, the example here v.qq returns a B object, and then the assignment  
kicks in, therefore it calls B.opAssign(5), thus the i member of that B  
instance is 5.


-- 
使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/



More information about the Digitalmars-d mailing list