opDispatch is grand!

BCS none at anon.com
Sat Apr 10 19:04:32 PDT 2010


Hello Ellery,

> 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
> 

I think you might be able to do:

ret!(T) opDispatch(string s, T)(T t) { ... }

and then switch internally based on T.

-- 
... <IXOYE><






More information about the Digitalmars-d mailing list