digitalmars.D - Discrete semantics of lambda expressions
- Xinok (23/23) May 02 2016 D has a few ways of writing lambda expressions / anonymous
- Adam D. Ruppe (2/5) May 02 2016 I agree, forcing people to rewrite it is a good idea.
- Timon Gehr (3/8) May 02 2016 I don't think { ... } as shorthand for (){ ... } is necessary or
- Adam D. Ruppe (5/7) May 02 2016 Indeed. I don't think =>x as a shorthand for {return x;} is
- Timon Gehr (8/15) May 02 2016 function int delegate()delegate(int)(int y){ return (x)const=>{ return
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(); })
May 02 2016
On Monday, 2 May 2016 at 15:52:34 UTC, Xinok wrote: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:I agree, forcing people to rewrite it is a good idea.
May 02 2016
On 02.05.2016 19:31, Adam D. Ruppe wrote:On Monday, 2 May 2016 at 15:52:34 UTC, Xinok wrote:I don't think { ... } as shorthand for (){ ... } is necessary or particularly useful in the first place.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:I agree, forcing people to rewrite it is a good idea.
May 02 2016
On Monday, 2 May 2016 at 20:11:53 UTC, Timon Gehr wrote:I don't think { ... } as shorthand for (){ ... } is necessary or particularly useful in the first place.Indeed. I don't think =>x as a shorthand for {return x;} is really worth it either... D has a ridiculous number of variations on function syntax. But I don't expect any of them to be removed either.
May 02 2016
On 02.05.2016 22:20, Adam D. Ruppe wrote:On Monday, 2 May 2016 at 20:11:53 UTC, Timon Gehr wrote:function int delegate()delegate(int)(int y){ return (x)const=>{ return x+y;}; }; // :o) (=>x is not valid.)I don't think { ... } as shorthand for (){ ... } is necessary or particularly useful in the first place.Indeed. I don't think =>x as a shorthand for {return x;} is really worth it either... D has a ridiculous number of variations on function syntax. ...But I don't expect any of them to be removed either.Most of them don't hurt much (unlike { ... }). It's quite annoying that function definition syntax and delegate literal syntax are inconsistent though. E.g. lambda syntax should be usable for named functions (int x()=>3;).
May 02 2016