struct M{
T opIndex(uint i){ ... }
}
which gives me this:
m[i]
but what if I want it in two dimension so that I could do:
m[i][j]
is there anyway to do this?
Yes, you can do the C++ way of returning a temporary object (struct is best in D's case) which also has an index operator overload.
But a better idea in D is to go for the syntax m[i, j]
:
struct S
{
uint opIndex(uint i, uint j)
{
return i + j;
}
}
void main()
{
S s;
assert(s[2, 3] == 5);
}
If you still want to use m[i][j]
, a nested struct gives you some syntactic leeway:
struct S
{
auto opIndex(uint i)
{
struct Temp
{
uint opIndex(uint j)
{
return i + j;
}
}
return Temp();
}
}
void main()
{
S s;
assert(s[2][3] == 5);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With