I'm currently learning D right now, and I was wondering why certain functions like std.stdio.readln can work both with and without parenthesis for the function call. I've tried looking through the documentation but can't find an explanation for why you can use it without parenthesis.
Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 13, 2021 Inclusion of Parenthesis on Certain Functions | ||||
---|---|---|---|---|
| ||||
June 13, 2021 Re: Inclusion of Parenthesis on Certain Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Choi | On Sunday, 13 June 2021 at 15:45:53 UTC, Justin Choi wrote: >I'm currently learning D right now, and I was wondering why certain functions like std.stdio.readln can work both with and without parenthesis for the function call. I've tried looking through the documentation but can't find an explanation for why you can use it without parenthesis. Uniform Function Call Syntax (UFCS) https://tour.dlang.org/tour/en/gems/uniform-function-call-syntax-ufcs |
June 13, 2021 Re: Inclusion of Parenthesis on Certain Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Choi | On Sun, Jun 13, 2021 at 03:45:53PM +0000, Justin Choi via Digitalmars-d-learn wrote: > I'm currently learning D right now, and I was wondering why certain functions like std.stdio.readln can work both with and without parenthesis for the function call. I've tried looking through the documentation but can't find an explanation for why you can use it without parenthesis. In D, parentheses for function calls are optional when there are no arguments. So: func(); can be shortened to: func; When UFCS is in effect, the empty parentheses on the right side of the function call are also optional: func(abc); can be rewritten as: abc.func; T -- People demand freedom of speech to make up for the freedom of thought which they avoid. -- Soren Aabye Kierkegaard (1813-1855) |
June 13, 2021 Re: Inclusion of Parenthesis on Certain Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Choi | On Sunday, 13 June 2021 at 15:45:53 UTC, Justin Choi wrote: >I've tried looking through the documentation but can't find an explanation for why you can use it without parenthesis. https://dlang.org/spec/function.html#optional-parenthesis (Some exceptions regarding delegates and function pointers exist.) —Bastiaan |