templated lambda with {} cause GC

Paul Backus snarwin at gmail.com
Fri Aug 10 10:08:15 UTC 2018


On Friday, 10 August 2018 at 09:57:53 UTC, learnfirst1 wrote:
>
> import core.stdc.stdio;
>
> struct Test {
> 	string name ;
> }
>
> void T(alias pred, A...)(){
> 	__gshared t = Test(A) ;
> 	 pred(t);
> }
>
> extern(C) void main(){
> 	T!(t => printf("test 1 name = %s\n".ptr, t.name.ptr), "test") 
> ;  // build OK
> 	T!(t => {
> 		printf("test 2 name = %s\n".ptr, t.name.ptr);
> 	}, "test") ; // build error
> }
>
> --------------

The arrow syntax doesn't work for multi-line lambdas; you need to 
write them using what the spec calls "function literal" syntax 
[1]:

     T!((t) { // <- Parens around the argument, no arrow
         printf("test 2 name = %s\n".ptr, t.name.ptr);
     }, "test") ;

[1] https://dlang.org/spec/expression.html#function_literals


More information about the Digitalmars-d-learn mailing list