D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 16075 - Compiler rejects basic mixin template
Summary: Compiler rejects basic mixin template
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-25 19:58 UTC by Max Samukha
Modified: 2020-03-21 03:56 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Max Samukha 2016-05-25 19:58:15 UTC
mixin template Foo(A...) {
    static if (A.length) {
        void foo(A[0] a) {
        }

        mixin Foo!(A[1..$]) F;
        alias foo = F.foo;
    }
}

mixin Foo!(int, string, bool);

void main() {
}

Error: alias test.Foo!(int, string, bool).Foo!(string, bool).Foo!bool.foo recursive alias declaration


The mixin should produce properly nested overload sets of foo:

foo(int); foo(string); foo(bool);
F.foo(string); F.foo(bool);
F.F.foo(bool);
Comment 1 basile-z 2016-05-25 21:23:11 UTC
(In reply to Max Samukha from comment #0)
> mixin template Foo(A...) {
>     static if (A.length) {
>         void foo(A[0] a) {
>         }
> 
>         mixin Foo!(A[1..$]) F;
>         alias foo = F.foo;
>     }
> }
> 
> mixin Foo!(int, string, bool);
> 
> void main() {
> }
> 
> Error: alias test.Foo!(int, string, bool).Foo!(string, bool).Foo!bool.foo
> recursive alias declaration
> 
> 
> The mixin should produce properly nested overload sets of foo:
> 
> foo(int); foo(string); foo(bool);
> F.foo(string); F.foo(bool);
> F.F.foo(bool);

Hi, there is an error in your code, the correct way to write your template is:



mixin template Foo(A...)
{
    static if (A.length)
    {
        void foo(A[0] a) {}
        static if (A.length > 1) // here !!
        {
            mixin Foo!(A[1..$]) F;
            alias foo = F.foo;
        }
    }
}

mixin Foo!(int,string,bool);

void main() {}



otherwise the instanciation continues endlessly with an empty A.
Comment 2 Max Samukha 2016-05-26 03:52:16 UTC
(In reply to b2.temp from comment #1)

> 
> Hi, there is an error in your code, the correct way to write your template
> is:
> 
> 
> 
> mixin template Foo(A...)
> {
>     static if (A.length)
>     {
>         void foo(A[0] a) {}
>         static if (A.length > 1) // here !!
>         {
>             mixin Foo!(A[1..$]) F;
>             alias foo = F.foo;
>         }
>     }
> }
> 
> mixin Foo!(int,string,bool);
> 
> void main() {}
> 
> 
> 
> otherwise the instanciation continues endlessly with an empty A.

No. The base case is when A.length is zero. Remove the alias, and the example will compile.
Comment 3 Max Samukha 2016-05-26 04:07:54 UTC
Oops. You are right. In the original example, the wrong foo is aliased because the base case doesn't have one.