Thread overview
why mixin template can not inclulde statement;
Aug 10, 2018
learnfirst1
Aug 10, 2018
learnfirst1
Aug 10, 2018
Kagamin
Aug 10, 2018
learnfirst1
Aug 10, 2018
learnfirst1
Aug 10, 2018
Kagamin
Aug 10, 2018
Kagamin
Aug 10, 2018
Radu
Aug 10, 2018
learnfirst1
August 10, 2018
mixin template test(A...){
        __gshared a = A;
        a++;
}

extern(C) void main(){
        mixin test!123;
}

-------------

I dont want to use string mixin to intro a lot un want symbol,  try with mixin template find this not work.

I know if mixin test on global it should not working, but why not make the limit for function scope ?


I try use D for a WASM project then find so much limitation with no good reason.  or I missing some thing here ?


August 10, 2018
On Friday, 10 August 2018 at 12:38:55 UTC, learnfirst1 wrote:
> mixin template test(A...){


mixin template test(A...){
        __gshared a = A;
}

extern(C) void main(){
        mixin test!123;
}

---------------

duplicate symbol __D4test4mainUZ8__mixin111__a_field_0i in:
    test.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: linker exited with status 1


this really make no sense,  what is wrong with it?    (same code build with ldc2)
August 10, 2018
Mixim template can only introduce declarations, not statements, a workaround is a lambda called in place.

mixin template test(A...){
        __gshared a = A;
        int dummy = (){ a++; return 0; }();
}

extern(C) void main(){
        mixin test!123;
}
August 10, 2018
On Friday, 10 August 2018 at 13:05:24 UTC, Kagamin wrote:
> Mixim template can only introduce declarations, not statements, a workaround is a lambda called in place.
>
> mixin template test(A...){
>         __gshared a = A;
>         int dummy = (){ a++; return 0; }();
> }
>
> extern(C) void main(){
>         mixin test!123;
> }

Except that you can't use tuples for that, a working sample:

mixin template test(alias A){
        __gshared a = A;
        int dummy = (){ a++; return 0; }();
}

extern(C) void main(){
    mixin test!123;

    import core.stdc.stdio;
    printf("%d", a);
}
August 10, 2018
On Friday, 10 August 2018 at 13:05:24 UTC, Kagamin wrote:
> Mixim template can only introduce declarations, not statements, a workaround is a lambda called in place.
>
> mixin template test(A...){
>         __gshared a = A;
>         int dummy = (){ a++; return 0; }();
> }
>
> extern(C) void main(){
>         mixin test!123;
> }

Thanks, this work for me.   my second example should be a dmd bug ? (ldc work)

August 10, 2018
On Friday, 10 August 2018 at 13:01:21 UTC, learnfirst1 wrote:
> duplicate symbol __D4test4mainUZ8__mixin111__a_field_0i in:
>     test.o
> ld: 1 duplicate symbol for architecture x86_64
> clang: error: linker command failed with exit code 1 (use -v to see invocation)
> Error: linker exited with status 1
>
>
> this really make no sense,  what is wrong with it?    (same code build with ldc2)

Looks like some problem with tuple, try __gshared a = A[0];
August 10, 2018
On Friday, 10 August 2018 at 13:10:57 UTC, Kagamin wrote:
> On Friday, 10 August 2018 at 13:01:21 UTC, learnfirst1 wrote:
> Looks like some problem with tuple, try __gshared a = A[0];

this work,  it report no error but give  a link problem.  (this could be a bug ?)
August 10, 2018
On Friday, 10 August 2018 at 13:17:13 UTC, learnfirst1 wrote:
> this work,  it report no error but give  a link problem.  (this could be a bug ?)

mixin template test(A...){
        __gshared int a = A[0];
        pragma(inline, true) // remove this will work
        static extern(C) int test(){
                a++;
                return 0;
        }
        int dummy = test();
}

import core.stdc.stdio;
extern(C) void main(){
    mixin test!1;
    printf("a=%d\n", a);
}

-----------------------
Undefined symbols for architecture x86_64:
  "__D4test4mainUZ8__mixin1QvUNbNiZi", referenced from:
      _main in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: linker exited with status 1
August 10, 2018
try this:

mixin template test(A...){
        __gshared int a = A[0];
        int dummy = (){
                a++;
                return 0;
        }();
}

import core.stdc.stdio;
int main(){
    mixin test!1;
    printf("a=%d\n", a);
    return 0;
}