May 25, 2017
On Thursday, 25 May 2017 at 13:53:01 UTC, ag0aep6g wrote:
> On 05/25/2017 03:13 PM, Moritz Maxeiner wrote:
>> After thinking about this a bit I think I know why it doesn't work without static and it's not a compiler bug. Since
>> 
>> ---
>> string AutoConstructor(fields ...)() {}
>> ---
>> 
>> is just syntax sugar for
>> ---
>> template AutoConstructor(fields ...)
>> {
>>      string AutoConstructor() {}
>> }
>> ---
>> 
>> instantiating the template AutoConstructor inside class Person gives you a non-static member function AutoConstructor of class Person, so obviously we need an instance of Person to call it on. Or make it a static member function.
>
> I don't think that's it.
>
> The function itself is not mixed into the class. It's called and the result is mixed in. I don't see how it makes sense if the compiler tries to turn the called function into a method.

Well, then I guess we need a compiler guy to clear this up, because from my point of view, the template is instantiated within the scope of the class (way before we reach the mixin), nesting the template's scope within the class' scope, which makes the function within that template's scope a member function of the class.
May 25, 2017
On 05/25/2017 08:14 PM, Moritz Maxeiner wrote:
> Well, then I guess we need a compiler guy to clear this up, because from my point of view, the template is instantiated within the scope of the class (way before we reach the mixin), nesting the template's scope within the class' scope, which makes the function within that template's scope a member function of the class.

You get a very similar error when you instantiate outside:

----
class Person { int age; }
string AutoConstructor(fields ...)() { return ""; }
enum s = AutoConstructor!(Person.age);
    /* Error: need 'this' for 'AutoConstructor' of type 'string()' */
----

So I don't think it has anything to do with having the instantiation inside the class. Passing a class member to a function template seems to be the trigger. But it's more complicated than that. The instantiation itself goes through. The error only occurs when you actually call the function.

Also, simply instantiating a function template inside a class doesn't result in a method. If it did, the function/method should be able to access class members. But it can't:

----
int ft()() { return age; } /* Error: undefined identifier age */
class Person
{
    int age = 42;
    alias method = ft!(); /* error instantiating */
}
----
May 25, 2017
On Thursday, 25 May 2017 at 19:09:06 UTC, ag0aep6g wrote:
> [...]
> 
> Also, simply instantiating a function template inside a class doesn't result in a method. If it did, the function/method should be able to access class members. But it can't:
>
> ----
> int ft()() { return age; } /* Error: undefined identifier age */
> class Person
> {
>     int age = 42;
>     alias method = ft!(); /* error instantiating */
> }
> ----

Ok, you are right. Open a bug report?
May 25, 2017
On 05/25/2017 09:15 PM, Moritz Maxeiner wrote:
> Ok, you are right. Open a bug report?

Sure. https://issues.dlang.org/show_bug.cgi?id=17435
1 2
Next ›   Last »