digitalmars.D.learn - Empty functions
- Jan =?UTF-8?B?SMO2bmln?= (13/13) Oct 29 2020 I have asked this on StackOverflow[1]. I have received a valid
- rikki cattermole (10/10) Oct 29 2020 (Params){ FunctionBody; }
- Jan =?UTF-8?B?SMO2bmln?= (8/13) Oct 29 2020 This would mean, that this one should work as well.
- Jan =?UTF-8?B?SMO2bmln?= (4/5) Oct 29 2020 It does not work as I intended, as `() => {}` has not the return
- rikki cattermole (10/17) Oct 29 2020 alias RT = void function();
- Paul Backus (8/13) Oct 29 2020 Arrow lambdas take a single *expression* on the right-hand side,
- H. S. Teoh (8/15) Oct 29 2020 What you want is `() {}`, which is equivalent to `void function() {}`.
I have asked this on StackOverflow[1]. I have received a valid answer, which solves my problem, however, I have still not understood, why some versions of it work and some don't. The code is here[2]. I don't understand why `a` compiles just fine, while `b` and `c` don't. I think, that I don't understand what `void function()` does or is. Can someone explain it to me? And I don't see why `(){}` works and `() => {}` does not. [1]: https://stackoverflow.com/questions/64581514/void-lambda-function/64587101#64587101 [2]: https://run.dlang.io/is/GFe9Ht
Oct 29 2020
(Params){ FunctionBody; } Rule: ref|opt ParameterWithMemberAttributes FunctionLiteralBody https://dlang.org/spec/expression.html#function_literals void function() Is a type https://dlang.org/spec/type.html#delegates () => {} Is actually: () => Expression Rule: ref|opt ParameterWithMemberAttributes => AssignExpression https://dlang.org/spec/expression.html#lambdas
Oct 29 2020
On Thursday, 29 October 2020 at 08:48:59 UTC, rikki cattermole wrote:() => {} Is actually: () => Expression Rule: ref|opt ParameterWithMemberAttributes => AssignExpression https://dlang.org/spec/expression.html#lambdasThis would mean, that this one should work as well. And you can![1] I have changed line 13 from `F();` to `return F();`. Why does this help??? This is a little weird. [1]: https://run.dlang.io/is/eGah5v
Oct 29 2020
On Thursday, 29 October 2020 at 09:01:12 UTC, Jan Hönig wrote:This would mean, that this one should work as well.It does not work as I intended, as `() => {}` has not the return type of `void`. (I don't know how to print: `ReturnType!(() => {})`)
Oct 29 2020
On 29/10/2020 10:06 PM, Jan Hönig wrote:On Thursday, 29 October 2020 at 09:01:12 UTC, Jan Hönig wrote:alias RT = void function(); alias Type = RT function(); () => 7; is equivalent to: int func() { return 7; } Or: () { return 7; }This would mean, that this one should work as well.It does not work as I intended, as `() => {}` has not the return type of `void`. (I don't know how to print: `ReturnType!(() => {})`)
Oct 29 2020
On Thursday, 29 October 2020 at 09:06:21 UTC, Jan Hönig wrote:On Thursday, 29 October 2020 at 09:01:12 UTC, Jan Hönig wrote:Arrow lambdas take a single *expression* on the right-hand side, not a function body between curly braces. When you write () => {} the right-hand side is interpreted as an expression, and is expanded by the compiler to () => function void() {} I.e., it is a function that returns another function.This would mean, that this one should work as well.It does not work as I intended, as `() => {}` has not the return type of `void`. (I don't know how to print: `ReturnType!(() => {})`)
Oct 29 2020