Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 03, 2015 Calling Syntax (no, not UFCS) | ||||
---|---|---|---|---|
| ||||
Hello! I'm new in D and it is amazing! Can you tell me please if it is discouraged or deprecated to call a function by just putting its name, without brackets? It's quite unusual for me (used C++ and Python before), but I can see this practice even in the official Phobos documentation: ``` foreach (result; [ 1, 2, 3, 4 ].map!("a + a", "a * a")) ... ``` The code `.map!("a + a", "a * a")()` also compiles and works as expected, of course. |
August 03, 2015 Re: Calling Syntax (no, not UFCS) | ||||
---|---|---|---|---|
| ||||
Posted in reply to SirNickolas | On Monday, 3 August 2015 at 22:42:15 UTC, SirNickolas wrote: > Hello! I'm new in D and it is amazing! > > Can you tell me please if it is discouraged or deprecated to call a function by just putting its name, without brackets? It's quite unusual for me (used C++ and Python before), but I can see this practice even in the official Phobos documentation: > > ``` > foreach (result; [ 1, 2, 3, 4 ].map!("a + a", "a * a")) > ... > ``` > > The code `.map!("a + a", "a * a")()` also compiles and works as expected, of course. http://dlang.org/function.html#optional-parenthesis Also note http://dlang.org/function.html#property-functions |
August 03, 2015 Re: Calling Syntax (no, not UFCS) | ||||
---|---|---|---|---|
| ||||
Posted in reply to SirNickolas | On Monday, 3 August 2015 at 22:42:15 UTC, SirNickolas wrote:
> Hello! I'm new in D and it is amazing!
>
> Can you tell me please if it is discouraged or deprecated to call a function by just putting its name, without brackets? It's quite unusual for me (used C++ and Python before), but I can see this practice even in the official Phobos documentation:
>
> ```
> foreach (result; [ 1, 2, 3, 4 ].map!("a + a", "a * a"))
> ...
> ```
>
> The code `.map!("a + a", "a * a")()` also compiles and works as expected, of course.
Opinions are mixed, there is no clear consensus, it probably doesn't matter that much.
I try to make sure to put the parens in for a function that's really "doing" something, like modifying global state or doing non-trivial amounts of work, but apart from that I'm very inconsistent.
|
August 03, 2015 Re: Calling Syntax (no, not UFCS) | ||||
---|---|---|---|---|
| ||||
Posted in reply to SirNickolas | On Mon, 03 Aug 2015 22:42:14 +0000, SirNickolas wrote: > Hello! I'm new in D and it is amazing! > > Can you tell me please if it is discouraged or deprecated to call a function by just putting its name, without brackets? It's quite unusual for me (used C++ and Python before), but I can see this practice even in the official Phobos documentation: > > ``` > foreach (result; [ 1, 2, 3, 4 ].map!("a + a", "a * a")) > ... > ``` > > The code `.map!("a + a", "a * a")()` also compiles and works as > expected, of course. Opinions vary, but I think it's generally idiomatic to omit empty parens when chaining but otherwise include them. E.g.: void foo() { ... } void main() { foo; // don't do this foo(); // do this // Empty parens in a chain are just noise: [1,2,3].map!(i => i + 1)() .reduce!`a+b`() .writeln(); // This is better [1,2,3].map!(i => i + 1) .reduce!`a+b` .writeln(); // you may or may not want to conclude with parens } One gotcha that still gets me is with sort: somearray.sort; // calls the builtin property sort left over from D1, don't use! somearray.sort(); // calls std.algorithm.sort with default `a<b` comparator So: somearray.map(i => i+1).array.sort().reduce!`a+b`.writeln(); |
August 04, 2015 Re: Calling Syntax (no, not UFCS) | ||||
---|---|---|---|---|
| ||||
Posted in reply to SirNickolas | On Monday, 3 August 2015 at 22:42:15 UTC, SirNickolas wrote: > Hello! I'm new in D and it is amazing! > > Can you tell me please if it is discouraged or deprecated to call a function by just putting its name, without brackets? It's quite unusual for me (used C++ and Python before), but I can see this practice even in the official Phobos documentation: > > ``` > foreach (result; [ 1, 2, 3, 4 ].map!("a + a", "a * a")) > ... > ``` > > The code `.map!("a + a", "a * a")()` also compiles and works as expected, of course. http://nomad.so/2013/08/alternative-function-syntax-in-d/ |
Copyright © 1999-2021 by the D Language Foundation