Trass3r <u...@known.com> wrote:

I also wanted to allow for swizzled setters, but apparently that's
currently not possible[1].

[1]: http://d.puremagic.com/issues/show_bug.cgi?id=620

What a pity!

Actually, after a bit of hacking, I have found a way:


// new helper function
static private bool noCharsRepeated( string s )
{
        foreach ( i, e1; s[0..$-1])
                foreach ( e2; s[i+1..$] )
                        if ( e1 == e2 )
                                return false;
        return true;
}

/// swizzling
template opDispatch( string n ) {
        @property Vector!(T,n.length) opDispatch( U... )( U args )
if (allCharsValid(n,"xyzw"[0..dim]) && (U.length == 0 || (U.length == 1 && is(U[0] == Vector!(T,n.length)) && noCharsRepeated(n))))
        {
                static if ( U.length == 1 ) {
                        static if ( n.length >= 1 )
                                cell[n[0]-'x'] = args[0].cell[0];
                        static if ( n.length >= 2 )
                                cell[n[1]-'x'] = args[0].cell[1];
                        static if ( n.length >= 3 )
                                cell[n[2]-'x'] = args[0].cell[2];
                        static if ( n.length >= 4 )
                                cell[n[3]-'x'] = args[0].cell[3];
                        
                }
                static if (n.length == 2) return
                        Vector!(T,n.length)(cell[n[0]-'x'], cell[n[1]-'x']);
                static if (n.length == 3) return
                        Vector!(T,n.length)(cell[n[0]-'x'], cell[n[1]-'x'], 
cell[n[2]-'x']);
                static if (n.length == 4) return
Vector!(T,n.length)(cell[n[0]-'x'], cell[n[1]-'x'], cell[n[2]-'x'], cell[n[3]-'x']);
        }
}


The signature might not exactly be clean, but it works.

--
Simen

Reply via email to