April 17, 2009 Fully dynamic d by opDotExp overloading | ||||
---|---|---|---|---|
| ||||
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?
--
使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
|
April 17, 2009 Re: Fully dynamic d by opDotExp overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to davidl | Very cool. What I'd like to see is this, TypeInfo_Struct.getMembers and ClassInfo.getMembers. Once we have all three, we could do some incredibly awesome things. Like implement Objective C-style message passing as a library. :D -- Daniel |
April 17, 2009 Re: Fully dynamic d by opDotExp overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to davidl | davidl wrote: > After tweaking dmd a bit litte, i get the dotexp overloading work. > > class c > { > > B opDotExp(char[] methodname,...) > { > writefln("god it works ", methodname); > return new B(); > } > it generates the output: > god it works dynamicmethod > god it works qq > 5 > > Any comments? Do you like this feature? That's very impressive. How did you do it? Are the syntax and semantic passes still independent? |
April 17, 2009 Re: Fully dynamic d by opDotExp overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don | 在 Fri, 17 Apr 2009 14:52:30 +0800,Don <nospam@nospam.com> 写道: > davidl wrote: >> After tweaking dmd a bit litte, i get the dotexp overloading work. >> class c >> { >> B opDotExp(char[] methodname,...) >> { >> writefln("god it works ", methodname); >> return new B(); >> } > >> it generates the output: >> god it works dynamicmethod >> god it works qq >> 5 >> Any comments? Do you like this feature? > > That's very impressive. How did you do it? Are the syntax and semantic passes still independent? Yes, it follows the rule of rewriting the c.unknownmethod(args) to c.opDotExp("unknownmethod", args); Though it currently gets bugs of not supporting struct, opDotExp signiture must be (char[],...), and possibly break extension method. These should all be fixable. -- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/ |
April 17, 2009 Re: Fully dynamic d by opDotExp overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | 在 Fri, 17 Apr 2009 14:44:46 +0800,Daniel Keep <daniel.keep.lists@gmail.com> 写道: > > Very cool. What I'd like to see is this, TypeInfo_Struct.getMembers and > ClassInfo.getMembers. Once we have all three, we could do some > incredibly awesome things. > > Like implement Objective C-style message passing as a library. :D > > -- Daniel Thanks. I also hope for more runtime info. -- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/ |
April 17, 2009 Re: Fully dynamic d by opDotExp overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to davidl | davidl:
> Any comments? Do you like this feature?
It's cute.
Maybe "." can be used for static methods and some other syntax for dynamic ones, like:
v..dynamicmethod(x);
v->dynamicmethod(x);
It seems that after releasing DMD source code the head of the cat is finally starting to come out of Walter's bag :-) This is for the better.
Bye,
bearophile
|
April 17, 2009 Re: Fully dynamic d by opDotExp overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to davidl | 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 |
April 17, 2009 Re: Fully dynamic d by opDotExp overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Fri, 17 Apr 2009 13:25:45 +0400, Andrei Alexandrescu <SeeWebsiteForEmail@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). |
April 17, 2009 Re: Fully dynamic d by opDotExp overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to davidl | Op Fri, 17 Apr 2009 08:31:02 +0200 schreef davidl <davidl@nospam.org>: > it generates the output: > god it works dynamicmethod > god it works qq > 5 > > Any comments? Do you like this feature? > Awesome, this is exactly what I thought was the point of 'opDot' when i first skimmed over it in the docs. This is great for lots of purposes. A small nice example is: http://haxe.org/doc/advanced/xml_fast |
April 17, 2009 Re: Fully dynamic d by opDotExp overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to davidl | davidl wrote:
> After tweaking dmd a bit litte, i get the dotexp overloading work.
Do want.
With this and extended runtime information, you can create a Variant that works exactly like the original type, allowing you to call methods on it without extracting it to the original type, just throwing an ArgumentException or MethodMissingException if you give it something bad.
|
Copyright © 1999-2021 by the D Language Foundation