| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
January 10, 2011 Templated delegate | ||||
|---|---|---|---|---|
| ||||
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);
writeln(result);
}
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?
| ||||
January 10, 2011 Re: Templated delegate | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mitja | On 10/01/2011 13:02, Mitja wrote: > I would like to have a template which returns > appropriate delegate, something like this: For what purpose? All you seem to have is a long-winded identity function. <snip> > and compiled it like this: dmd mod2.d mod1.d, the program would produce > segmentation fault. I can't reproduce (DMD 2.051, Windows). In what setup are you experiencing the problem? > It works fine when packages in mod2 are omitted. When which packages in mod2 are omitted? I can only make out that this is a compiler bug that needs to be investigated. > What would be the correct way for templated delegate? It depends on what you're trying to do. Stewart. | |||
January 10, 2011 Re: Templated delegate | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mitja | Mitja <odtihmal@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 | |||
January 10, 2011 Re: Templated delegate | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | 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@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
| |||
January 10, 2011 Re: Templated delegate | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Stewart Gordon Wrote: > On 10/01/2011 13:02, Mitja wrote: > > I would like to have a template which returns > > appropriate delegate, something like this: > > For what purpose? All you seem to have is a long-winded identity function. > > <snip> > > and compiled it like this: dmd mod2.d mod1.d, the program would produce segmentation fault. > > I can't reproduce (DMD 2.051, Windows). In what setup are you experiencing the problem? The problem appears in the following setup: Debian GNU/Linux 5.0, kernel 2.6.26-2-686 #1 SMP i686. dmd is D 2.051. As well as on Windows XP as guest OS in VirtualBox. XP prints out: "object.Error: Access Violation" > > > It works fine when packages in mod2 are omitted. > > When which packages in mod2 are omitted? > > I can only make out that this is a compiler bug that needs to be investigated. > > > What would be the correct way for templated delegate? > > It depends on what you're trying to do. > > Stewart. | |||
January 11, 2011 Re: Templated delegate | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mitja | 10.01.2011 21:08, Mitja пишет:
> 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.
>
Try it with previous compiler version. It looks like a bug I've reported not so long ago: in 2.051 functions with 'auto' result type and at least one parameter cause segfault during compilation when they are in a separate module. I can't remember issue number right now (and have no internet connection to find it), but it's pretty recent.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply