On Friday, 8 November 2013 at 04:28:31 UTC, Ross Hays wrote:
Strange. I'm getting a different error, but I'm still running
2.063.2.
The error I get is
`Error: cannot resolve type for t.opDispatch!("x")`
What version are you running?
I just updated to 2.064.2
In any case, the reason apparently is multifold:
1. Apparently the proper error message isn't shown when using
the property notation. (I'd have to check to see if it happens
in 2.064 ... might be a fixed bug)
2. `.charAt(0)` doesn't exist for D's strings. You can just
use bracket notation to access the index.
3. When args is empty (as it will be for a getter, when you
call) args[0] doesn't exist, so `Error: array index [0] is
outside array bounds [0 .. 0]`
So fix 2 and 3 and it works for getting x. The reason I use a
static if is to separate the cases where args has items and
when it does not (since args[0] is invalid when args.length ==
0), so that'll be necessary to get it to work.
Okay 2 is fixed, leftovers of other languages in my mind.
Also took care of 3 I think but I may still be missing
something.
Here is what I have now..
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 &&
toOffset(fieldName) < N)
{
int offset = fieldName[0 .. 1] - 'x';
if (args.length != 0)
return data[offset];
else
return data[offset] = args[0];
}
}
Same error.
Just reread and realized I glossed over your mention of static if.
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 &&
toOffset(fieldName) < N)
{
int offset = fieldName[0 .. 1] - 'x';
static if (args.length != 0)
return data[offset];
else
return data[offset] = args[0];
}
}
Still the same problems.