www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Confusion with anonymous functions and method overloads

reply pineapple <meapineapple gmail.com> writes:
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!
May 21 2016
next sibling parent Anonymouse <asdf asdf.net> writes:
On Saturday, 21 May 2016 at 14:39:59 UTC, pineapple wrote:
     void clean(in void delegate(in T value) func){
         this.clean((in T values[]) => {
             foreach(value; values) func(value);
         });
This doesn't do what you think it does. It passes a lambda that *returns* that foreach function (that returns void).
     void clean(in void delegate(in T value) func){
         this.clean((in T values[]){
             foreach(value; values) func(value);
         });
     }
This passes a lambda (that returns void). See https://dpaste.dzfl.pl/f93b9c0c8426
May 21 2016
prev sibling parent ag0aep6g <anonymous example.com> writes:
On 05/21/2016 04:39 PM, pineapple wrote:
 But I don't understand why. Could someone clarify the difference between
 the two?
Common mistake, because other languages (e.g. C#) use similar but different syntax. The `foo => bar` syntax doesn't use braces. When you add braces around bar, that's a delegate that runs bar when called. I.e., this: foo => bar is equivalent to this: (foo) {return bar;} And this: foo => {bar;} is equivalent to this: (foo) {return () {bar;};}
May 21 2016