Thread overview
Empty functions
Oct 29, 2020
Jan Hönig
Oct 29, 2020
rikki cattermole
Oct 29, 2020
Jan Hönig
Oct 29, 2020
Jan Hönig
Oct 29, 2020
rikki cattermole
Oct 29, 2020
Paul Backus
Oct 29, 2020
H. S. Teoh
October 29, 2020
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

October 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
October 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#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



October 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!(() => {})`)
October 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:
>> 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; }
October 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:
>> 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.
October 29, 2020
On Thu, Oct 29, 2020 at 09:06:21AM +0000, Jan Hönig via Digitalmars-d-learn 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!(() => {})`)

What you want is `() {}`, which is equivalent to `void function() {}`.

`() => {}` means something different; it's equivalent to:

	() { return () {}; }

i.e., it's a function that returns an empty function.


T

-- 
Tell me and I forget. Teach me and I remember. Involve me and I understand. -- Benjamin Franklin