Discrete semantics of lambda expressions

Xinok via Digitalmars-d digitalmars-d at puremagic.com
Mon May 2 08:52:34 PDT 2016


D has a few ways of writing lambda expressions / anonymous 
functions:

     x => doSomething()
     { doSomething(); }
     (){ doSomething(); }

While the flexibility is great, there's a hidden issue for those 
programmers who come from different languages and are used to 
writing:

     x => { doSomething(); doSomethingElse(); }

At first glance, this may seem okay but what's actually happening 
is that this is a lambda returning a lambda. The correct way 
would be to rewrite this as one of:

     x => { doSomething(); doSomethingElse(); }()
     (x){ doSomething(); doSomethingElse(); }

This particular issue as popped up twice in the last couple days 
alone and presumably many more times in the past:

http://forum.dlang.org/thread/qsayoktyffczskrnmgxu@forum.dlang.org
http://forum.dlang.org/thread/thgyqyarccinzuqhcjtf@forum.dlang.org

I'm proposing that we add a warning to the compiler for this 
particular case. If the programmer intended to return a lambda, 
then rewrite the expression as one of:

     x => (){ doSomething(); doSomethingElse(); }
     x => ({ doSomething(); doSomethingElse(); })


More information about the Digitalmars-d mailing list