www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Empty functions

reply Jan =?UTF-8?B?SMO2bmln?= <hrominium gmail.com> writes:
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
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
(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
parent reply Jan =?UTF-8?B?SMO2bmln?= <hrominium gmail.com> writes:
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#lambdas
This 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
parent reply Jan =?UTF-8?B?SMO2bmln?= <hrominium gmail.com> writes:
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
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
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:
 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!(() => {})`)
alias RT = void function(); alias Type = RT function(); () => 7; is equivalent to: int func() { return 7; } Or: () { return 7; }
Oct 29 2020
prev sibling next sibling parent Paul Backus <snarwin gmail.com> writes:
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:
 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!(() => {})`)
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.
Oct 29 2020
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes: