Templated delegate

Mitja odtihmal at gmail.com
Mon Jan 10 10:08:25 PST 2011


Thanks, toDelegate is what I've been looking for.

It still segfaults, though.
The setup:
=======
module mod1;

import std.algorithm;
import std.functional;
import std.stdio;

void main( ) {
   auto haystack = ["a","b","c"];
   auto needle = "b";
   auto flt = delegate(string s){return s == needle;};
   auto dg = toDelegate( flt );
   auto result = filter!dg(haystack);
   writeln(result);
}
=========
module mod2;

import std.string;
// or import std.conv;
// or import std.random;
========
dmd mod2.d mod1.d compiles, but compiled program segfaults.
System is Debian GNU/Linux 5.0 on 2.6.26-2-686 #1 SMP i686.
dmd is D 2.051.


Simen kjaeraas Wrote:

> Mitja <odtihmal at gmail.com> wrote:
> 
> > I would like to have a template which returns
> > appropriate delegate, something like this:
> 
> So like this?
> 
> module mod1;
> 
> import std.algorithm;
> import std.functional;
> import std.stdio;
> 
> void main( ) {
>    auto haystack = ["a","b","c"];
>    auto needle = "b";
>    auto flt = (string s){return s == needle;};
>    auto dg = toDelegate( flt );
>    auto result = filter!dg(haystack);
>    writeln(result);
> }
> 
> I would have to argue that the delegate is hardly templated, though. Not
> sure what that means.
> 
> > What would be the correct way for templated delegate?
> 
> Depending on what you mean by 'templated delegate', I would say
> std.functional's toDelegate, as used above.
> 
> Perhaps if you explained what you mean by 'templated delegate', I could
> give a better answer.
> 
> -- 
> Simen



More information about the Digitalmars-d-learn mailing list