Templated delegate

Mitja odtihmal at gmail.com
Mon Jan 10 05:02:23 PST 2011

I would like to have a template which returns
appropriate delegate, something like this:

module mod1;

import std.algorithm;
import std.array;
import std.stdio;

template DG(RT, T, F)
  auto DG(F fun)
    RT delegate(T p)dg;
    dg = fun;
    return dg;

void main()
  string[] haystack = ["item 1", "item 2", "item 3"];
  string needle = "item 2";

  auto flt = (string s){return s == needle;};

  auto dg = DG!(bool, string, typeof(flt))(flt);

  auto result = filter!(dg)(haystack);


The thing is I don't know what am I doing wrong here,
because sometimes works and sometimes doesn't.
For instance, if I'd added another file along with mod1.d, let's say,
mod2.d which contained:

module mod2;

import std.conv;
import std.string;
import std.random;

and compiled it like this: dmd mod2.d mod1.d, the program would produce
segmentation fault.
It works fine when packages in mod2 are omitted.

What would be the correct way for templated delegate?

More information about the Digitalmars-d-learn mailing list