Template class with dispatched properties

Ross Hays throwaway at email.net
Thu Nov 7 20:52:06 PST 2013


Boom, that last few were the issues. I elected to just move the 
return for the setter onto a separate line, mostly because the 
idea of auto returning different types seem foreign to me... I 
have used auto plenty in C++11, but never like that and it just 
throws me off. But fixing those other mistakes we get:

class Vector(int N, T) if (N <= 3) {
     T[N] data;

     this()
     {
     	data[] = 0;
     }

     @property ref T opDispatch(string fieldName, Args ...)(Args 
args)
         if (Args.length < 2 && fieldName.length == 1 && 
fieldName[0] - 'x' < N)
     {
	    int offset = fieldName[0] - 'x';
	    static if (args.length == 0)
	    {
	    	return data[offset];
         }
	    else
	    {
	    	data[offset] = args[0];
	    	return data[offset];
         }
     }
}

void main()
{
	Vector!(2, float) t = new Vector!(2,float)();
	writeln(t.x);
	t.x = 4;
	writeln(t.x);
}

That works as intended, now if only it were useful.


More information about the Digitalmars-d-learn mailing list