opDispatch compiles fine, but still fails to resolve?
Vlad Levenfeld via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Aug 9 08:14:05 PDT 2014
On Saturday, 9 August 2014 at 10:36:55 UTC, Artur Skawina via
Digitalmars-d-learn wrote:
> 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
Yeah, sorry, I made that change last-minute to verify, with the
pragma, that I was indeed getting the correct type. The problem
must have been something else. I just made a complete oversight
in the process of throwing everything but the kitchen sink into
debugging it.
More information about the Digitalmars-d-learn
mailing list