Confusion with anonymous functions and method overloads

pineapple via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 21 07:39:59 PDT 2016


I wrote a pair of methods that looked like this:

     void clean(in void delegate(in T value) func){
         this.clean((in T values[]) => {
             foreach(value; values) func(value);
         });
     }
     void clean(in void delegate(in T values[]) func){
         ...
     }

I was getting a compile error on the second line of that example:

E:\Dropbox\Projects\d\mach\misc\refcounter.d(63): Error: none of 
the overloads of 'clean' are callable using argument types (void 
delegate() @system delegate(const(uint[]) values) pure nothrow 
@safe), candidates are:
E:\Dropbox\Projects\d\mach\misc\refcounter.d(62):        
mach.misc.refcounter.RefCounter!uint.RefCounter.clean(const(void 
delegate(const(uint))) func)
E:\Dropbox\Projects\d\mach\misc\refcounter.d(67):        
mach.misc.refcounter.RefCounter!uint.RefCounter.clean(const(void 
delegate(const(uint[]))) func)
E:\Dropbox\Projects\d\mach\misc\refcounter.d(109): Error: 
template instance mach.misc.refcounter.RefCounter!uint error 
instantiating

When I got rid of the "=>" and changed the first method to this, 
it compiled without issue:

     void clean(in void delegate(in T value) func){
         this.clean((in T values[]){
             foreach(value; values) func(value);
         });
     }

But I don't understand why. Could someone clarify the difference 
between the two?

Thanks!


More information about the Digitalmars-d-learn mailing list