Re: Empty functions

2020-10-29 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Oct 29, 2020 at 09:06:21AM +, 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


Re: Empty functions

2020-10-29 Thread Paul Backus via Digitalmars-d-learn

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.


Re: Empty functions

2020-10-29 Thread rikki cattermole via Digitalmars-d-learn

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; }


Re: Empty functions

2020-10-29 Thread Jan Hönig via Digitalmars-d-learn

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!(() => {})`)


Re: Empty functions

2020-10-29 Thread Jan Hönig via Digitalmars-d-learn
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





Re: Empty functions

2020-10-29 Thread rikki cattermole via Digitalmars-d-learn

(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