dynamic classes and duck typing
Don
nospam at nospam.com
Sat Nov 28 06:20:46 PST 2009
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>.
More information about the Digitalmars-d
mailing list