opDispatch compiles fine, but still fails to resolve?

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 9 03:36:42 PDT 2014


On 08/09/14 03:20, Vlad Levenfeld via Digitalmars-d-learn wrote:
> More opDispatch woes. This feature keeps biting me, yet I keep trying to use it.
> 
> This time I'm trying to access elements of a vector GLSL-style (without swizzling... for now).
> 
> Here's the relevant code:
> 
> struct Vector (uint length, Element = double)
> {
>         ref @property component (string c)()
>         {
>             enum mat = q{xyzw};
>             enum col = q{rgba};
>             enum tex = q{uv};
> 
>             static if (mat.canFind (c))
>                 return components[mat.countUntil (c)];
>             else static if (col.canFind (c))
>                 return components[col.countUntil (c)];
>             else static if (tex.canFind (c))
>                 return components[tex.countUntil (c)];
>             else static assert (0);
>         }
> 
>         ref @property opDispatch (string c)()
>         if (c.length == 1)
>         {
>             auto v = component!c;
>             pragma(msg, typeof(v)); // this outputs the expected result
>             return v; // so everything is fine, right? wrong...
>         }
> 
>     Element[length] components;
> }
> 
> Calling vector.component!`x` or something works fine, but calling vector.x fails with "Error: no property" etc.
> 
> Now, I'm used to opDispatch silently failing when it can't compile (very annoying btw), but in this case, I tested every line with a pragma (msg, __traits(compiles...)) and everything checks out. All expressions are verified CTFE-able. The compiler apparently makes it all the way through the method without a hitch, but then just fails symbol resolution anyway.
> 
> Is this just a bug? Does anyone have any advice on what else to try?

v.opDispatch!`x`;

Your opDispatch is returning a reference to a local stack-allocated
variable. D does not support real references; that 'auto v=...'
declaration creates a copy.

   ref @property opDispatch (string c)() { return component!c; }

[opDispatch works for every kind of symbol, there's no problem with
 @property]

artur


More information about the Digitalmars-d-learn mailing list