www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: dynamic classes and duck typing

reply bearophile <bearophileHUGS lycos.com> writes:
Walter Bright:
 and then s.foo(3), if foo is not a compile time member of s, is 
 rewritten as:
     s.opDynamic!("foo")(3);

I don't understand, isn't this the right translation? s.opDynamic("foo", 3); If the "foo" name is known at compile time only (as in all C# examples and in Python) you can't use a template argument. (And then it becomes useful to have associative arrays that are fast when the number of keys is small, < 10). Bye, bearophile
Nov 28 2009
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
bearophile:
 If the "foo" name is known at compile time only

I meant at "run-time only", that's what defines it as dynamic. Otherwise, as Michel Fortin says, there's nothing dynamic in it. Bye, bearophile
Nov 28 2009
prev sibling next sibling parent reply Don <nospam nospam.com> writes:
bearophile wrote:
 Walter Bright:
 and then s.foo(3), if foo is not a compile time member of s, is 
 rewritten as:
     s.opDynamic!("foo")(3);

I don't understand, isn't this the right translation? s.opDynamic("foo", 3); If the "foo" name is known at compile time only (as in all C# examples and in Python) you can't use a template argument. (And then it becomes useful to have associative arrays that are fast when the number of keys is small, < 10).

You mean, if it's known at run-time only? What Walter has written is definitely the correct one. Consider, for example, swizzling an array of float[4] using SSE2 Instead of writing the 256 functions .xyzw(), .xzyz(), .wzyy(), etc, you can just write a single function: bool isXYZW(char c) { return c=='x' || c=='y' || c=='z' || c=='w'); float[4] opDynamic(char [] s)() if (s.length==4 && isXYZW(s[0]) && isXYZW(s[1] && isXYZW(s[2]) && isXYZW { string hexdigit = "0123456789ABCDEF"; mixin("asm { pshufd xmm0, xmm1, 0x" ~ hexdigit[((s[0]-'w'+3)&3)*4+(s[1]-'w'+3)&3] ~ hexdigit[((s[2]-'w'+3)&3)*4+(s[3]-'w'+3)&3] ~";"); } Not actually tested <g>.
Nov 28 2009
parent reply bearophile <bearophileHUGS lycos.com> writes:
Don:
 You mean, if it's known at run-time only?

Yes, sorry.
 What Walter has written is definitely the correct one.

I am not saying that what Walter has written is wrong or useless :-) I am saying that it's not dynamic. This means that: 1) The name of that operator "opDynamic" is wrong, it needs a name that doesn't contain "dynamic" inside. "opDispatch" suggested by Michel Fortin seems OK. It can have that signature shown by Walter: T opDispatch(s : string)(args...); 2) A second operator like: variant opDynamic(string name, variant[]); can be added for the truly dynamic case, as in C#/Java and in dynamic languages. As I've said such operator will enjoy fast associative arrays when the number of keys is small (Python dicts have an optimization for such very common case).
you can just write a single function:<

That's not an example of the most readable code, but it's cute :-) Bye and thank you, bearophile
Nov 28 2009
parent bearophile <bearophileHUGS lycos.com> writes:
 It can have that signature shown by Walter:
 T opDispatch(s : string)(args...);

Or, to keep its name simpler to remember beside the opDynamic: T opStatic(s : string)(args...); Bye, bearophile
Nov 28 2009
prev sibling parent reply KennyTM~ <kennytm gmail.com> writes:
On Nov 28, 09 22:00, bearophile wrote:
 Walter Bright:
 and then s.foo(3), if foo is not a compile time member of s, is
 rewritten as:
      s.opDynamic!("foo")(3);

I don't understand, isn't this the right translation? s.opDynamic("foo", 3); If the "foo" name is known at compile time only (as in all C# examples and in Python) you can't use a template argument. (And then it becomes useful to have associative arrays that are fast when the number of keys is small,< 10). Bye, bearophile

Probably because you can write Variant myOpReallyDynamic(string name, Variant[] s...) { ... } Variant opDynamic(string name)(Variant[] s...) { return myOpReallyDynamic(name, s); } but not the other way round.
Nov 28 2009
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
KennyTM~ wrote:
 On Nov 28, 09 22:00, bearophile wrote:
 Walter Bright:
 and then s.foo(3), if foo is not a compile time member of s, is
 rewritten as:
      s.opDynamic!("foo")(3);

I don't understand, isn't this the right translation? s.opDynamic("foo", 3); If the "foo" name is known at compile time only (as in all C# examples and in Python) you can't use a template argument. (And then it becomes useful to have associative arrays that are fast when the number of keys is small,< 10). Bye, bearophile

Probably because you can write Variant myOpReallyDynamic(string name, Variant[] s...) { ... } Variant opDynamic(string name)(Variant[] s...) { return myOpReallyDynamic(name, s); } but not the other way round.

That is correct. Thanks for pointing that out. The operator is dynamic because it may perform a dynamic lookup under a static syntax. Straight dynamic invocation with a regular function has and needs no sugar. Andrei
Nov 28 2009