Fully dynamic d by opDotExp overloading
Denis Koroskin
2korden at gmail.com
Fri Apr 17 02:54:35 PDT 2009
On Fri, 17 Apr 2009 13:25:45 +0400, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> 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).
More information about the Digitalmars-d
mailing list