Template class with dispatched properties

Chris Cain clcain at uncg.edu
Thu Nov 7 19:48:46 PST 2013


On Friday, 8 November 2013 at 03:35:34 UTC, Ross Hays wrote:
> Awesome that seems to do what I was going for. I had tried a 
> similar approach with @property dispatch and the subtraction of 
> 'x', but I had left out the static if and had the opDispatch 
> returning a ref of the entry in the array (so there would just 
> be the one @property still) but that resulted in "Error: no 
> property 'x' for type 'test.Vector!(2, float).Vector'".
>
> This is interesting, though probably not a very safe way to 
> handle vectors in the real world (even more so if they are 
> going to be vectors of more than length 3).

Indeed. Presumably you could modify it to shift the starting 
letter (which is currently hardcoded as 'x') leftward as N grows 
bigger to allow it to support more. But really, you would 
probably be better to just use tuples. Technically a Vector is 
just a Tuple!(T, "x", T, "y") so you could probably do better by 
writing some metaprogramming to make one of those if you wanted a 
short-hand for it. Otherwise just doing "alias Vector2(T) = 
Tuple!(T, "x", T, "y");" would work great in 2.064.2, if I 
remember the syntax correctly. Then Vector2!float would work. 
Similarly Vector3 could be made and so on.

http://dlang.org/phobos/std_typecons.html#.Tuple
http://dlang.org/changelog.html#eponymous_template


More information about the Digitalmars-d-learn mailing list