opDispatch is grand!
Ellery Newcomer
ellery-newcomer at utulsa.edu
Sat Apr 10 18:38:57 PDT 2010
On 04/10/2010 05:49 PM, so wrote:
>
> struct vector(T, uint N) {
> auto opDispatch(string s) const {
> static if(N>0 && ("x"==s || "r"==s)) return v_[0];
> static if(N>1 && ("y"==s || "g"==s)) return v_[0];
> static if(N>2 && ("z"==s || "b"==s)) return v_[0];
> static if(N>3 && ("w"==s || "a"==s)) return v_[0];
> static assert("boom!");
> }
> private:
> T[N] v_;
> }
>
> No unions, clean and all!
> Then why do we need properties?
Can you get
vector!(int,3) v;
v.x = 1;
to work?
The closest I can get is
struct vector(T, uint N) {
ref T opDispatch(string s )() {
static if(N>0 && ("x"==s || "r"==s)) return v_[0];
static if(N>1 && ("y"==s || "g"==s)) return v_[1];
static if(N>2 && ("z"==s || "b"==s)) return v_[2];
static if(N>3 && ("w"==s || "a"==s)) return v_[3];
static assert("boom!");
}
private:
T[N] v_;
}
..
vector!(int, 3) v;
v.x() = 1;
when DMD sees
v.x = 1;
it tries to rewrite it as v.x(1);
And I can't figure out how to overload opDispatch
More information about the Digitalmars-d
mailing list