Template class with dispatched properties

Ross Hays throwaway at email.net
Thu Nov 7 20:38:22 PST 2013


> Sorry, I forgot to mention in that post that you have 
> "toOffset" in your template constraint, which means it will 
> also never match. You'll have to define it or replace it with 
> `fieldName[0] - 'x';`
>
> Also, you might not want to do `fieldName[0 .. 1]` because 
> that's a slice (which is just another array of length 1). It 
> won't do what you're expecting.

And more stupid mistakes on my part. Thank you, still not 100% 
there with D but working on it. (though some of those mistakes 
were dumb even outside of D lol).

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

     @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
	    	return data[offset] = args[0];
     }
}

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

Another revision, still the same problems and I am pretty sure I 
am not forgetting anything this time... I am sure that is wrong 
too


More information about the Digitalmars-d-learn mailing list