Template class with dispatched properties

Chris Cain clcain at uncg.edu
Thu Nov 7 20:47:48 PST 2013


On Friday, 8 November 2013 at 04:38:23 UTC, Ross Hays wrote:
> Thank you

No problem. I'm glad we're making progress on it. And don't feel 
bad about mistakes while learning. They happen. Embrace them 
because they happen to all of us at first especially when we're 
juggling learning new syntaxes and tricks and such.

>
> 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];
>     }
> }

Okay, your static if's condition is swapped (args.length should 
be 0 if you're just returning data[offset]).

Also, when you correct that, you'll run into an issue where it 
reveals that `data[offset] = args[0]` is not an lvalue (which is 
required for ref). Either change the return type to "auto" and 
remove the return for that branch... or, alternatively, you can 
split the statement into two:

else {
   data[offset] = args[0];
   return data[offset];
}

and `data[offset]` will be an lvalue.


More information about the Digitalmars-d-learn mailing list