Cannot use local lambda as parameter to non-global template

Ali Çehreli acehreli at yahoo.com
Mon Jan 15 17:20:51 UTC 2018


On 01/15/2018 07:27 AM, Meta wrote:
> On Monday, 15 January 2018 at 13:55:57 UTC, Nordlöw wrote:

>> The function `remove` is a templated member of a struct instance `x`.
> 
> Can you give a bit more context? The following code does not cause a 
> compile error:
> 
> import std.stdio;
> import std.algorithm;
> 
> void main()
> {
>      auto arr = [1, 2, 3];
>      arr.remove!(_ => _ == 1);
>      writeln(arr);
> }
> 
> Or was that code meant as an example?

import std.algorithm;

struct S {
     int[] elements;

     void remove(alias func)() {
         // ...
     }

     void remove(string s)() {
         foreach (a; elements) {
             const cond = mixin(s);
             if (cond) {
                 // do remove
             }
         }
     }
}

void main()
{
     auto s = S();
     s.remove!"a == 11"();

     // Fails compilation:
     // s.remove!(_ => _ == 1);
}

Ali


More information about the Digitalmars-d-learn mailing list