array of delegates

Alex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 13 16:39:35 PDT 2016


Hi at all!
Having read this:
http://forum.dlang.org/post/mailman.2415.1354291433.5162.digitalmars-d-learn@puremagic.com

still have a problem...
Lets begin with what works:

enum Props{p1, p2}

class AA
{
     int[] arr1;
     int[] arr2;
     this()
     {
         //arbitrary values...
         arr1 = [9, 6, 33, 42, 58];
         arr2 = [2, 3, 44, 28, 77];
     }

     auto Foo(int i, Props p)
     {
         with(Props)
         {
             final switch(p)
             {
                 case p1:
                     { return arr1[i]; }
                 break;
                 case p2:
                     { return arr2[i]; }
                 break;
             }
         }
     }
}

main()
{
     int indarr[] = [0, 1, 2, 3, 4];
     import std.stdio;
     import std.functional;
     AA aa = new AA();
     int delegate(int, Props) sg;
     sg = &aa.Foo;
     alias M3 = partial!(sg, 3);
     writeln(M3(Props.p1));
     writeln(M3(Props.p2));
}

So far, so good. But now the question:
if I try

import std.algorithm;
indarr.map!(a => partial!(sg, a));

I get an error, like Error: partial (Props _param_0) is not 
callable using argument types ().

Do you have a hint, what I'm doing wrong?

For the motivation: In this way I would like to store some 
"links" to data I have without having to define a struct, which 
incorporate the data.
The struct itself is not the problem, but how the data is handled 
is not known by the struct, but only by the class AA, so the 
expressions inside the switch should not (?) lie inside the 
struct.


More information about the Digitalmars-d-learn mailing list