[Issue 11946] "need 'this' to access member" when passing field to template parameter

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 23 10:24:16 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=11946



--- Comment #12 from Vladimir Panteleev <thecybershadow at gmail.com> 2014-01-23 20:24:12 EET ---
(In reply to comment #10)
>   // This is module level template function that have alias parameter.
>   int foo(alias sym)() { return sym; }
> 
>   class C
>   {
>     int n;
>     void test()
>     {
>       this.n = 10;
>       alias fooN = foo!(C.n);
>       // fooN has implicit context to access field variable n in class C.
>       static assert(is(typeof(&fooN) == delegate));
>       assert(fooN() == 10);
>     }
>   }

Thank you, I finally understand what's going on. But the situation looks really
bad. What if you want to pass the "n" variable from both the struct and the
function, to the same template? Or if the template already has a context
pointer (it is a templated method)? The context pointers should be tied to the
variables, not the same "this" pointer that is used in calling methods. Doing
it that way just produces absurd error messages.

In either case, at the very least the error message needs to be improved. The
simplest solution would be to just change the wording for such cases to make it
Google-able. But of course ideally the problem should be solved at its root.

> Again, current compiler does not consider that the context is not really used
> inside function.

Well, this is another bug then.

> > It is a free function!
> 
> No. It's a template function that is defined in module level.

I do not believe that the distinction needs to be forced upon the users.

> In many cases, learning it would not be necessary. But in your case the
> difference is necessary.
> Honestly, the alias parameter usage in your code does not fit the expected
> usage in language design.
> At least from D1 age, template alias parameter is designed to use the given
> symbol in runtime at instantiated code.

I just tested another bit of code:

struct O
{
    int f(A...)() { return 0; }

    struct S
    {
        int x;

        void g()
        {
            auto y = f!x();
        }
    }
}

With this one, we get:

test3.d(11,13): Error: template instance f!(x) cannot use local 'x' as
parameter to non-global template f(A...)()
test3.d(11,16): Error: this for f needs to be type O not type S

IMHO the correct solution would be to pass the context pointers of alias
parameters separately, so as to not interfere with any existing context
pointers of the template.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list