digitalmars.D.learn - calling functions without parentheses
- =?ISO-8859-1?Q?Adam_Cig=E1nek?= (13/13) Nov 08 2010 Hello,
- Steven Schveighoffer (11/23) Nov 08 2010 It is documented behavior in D1 and originally in D2. D2 will eventuall...
- =?ISO-8859-1?Q?Adam_Cig=E1nek?= (17/44) Nov 08 2010 Ok, thanks. I knew about @property, but not that it works even without
- Steven Schveighoffer (25/37) Nov 08 2010 The point is that parentheses imply a function, where lack-of implies
Hello, It seems that if function has no parameters, it's possible to omit the parentheses when calling it: string sayHello() { return "hello"; } void main() { writeln(sayHello); // same as writeln(sayHello()); } Is this an actual defined (and documented) behaviour that I can expect to keep working in the future, or just some random unintended side effect which might disappear in a next version? adam.
Nov 08 2010
On Mon, 08 Nov 2010 14:48:20 -0500, Adam Cigánek <adam.ciganek gmail.com> wrote:Hello, It seems that if function has no parameters, it's possible to omit the parentheses when calling it: string sayHello() { return "hello"; } void main() { writeln(sayHello); // same as writeln(sayHello()); } Is this an actual defined (and documented) behaviour that I can expect to keep working in the future, or just some random unintended side effect which might disappear in a next version?It is documented behavior in D1 and originally in D2. D2 will eventually require you to mark function calls omitting parentheses with property. This is valid D2 today, but the property part is not required to call the function: property string sayHello() { return "hello"; } D1 will remain the same, i.e. it will not require property. -Steve
Nov 08 2010
Ok, thanks. I knew about property, but not that it works even without it. Actually, is property even needed? I think that the ability to call functions without parentheses could be useful for non-property like function as well: widget.hide; instead of: widget.hide(); The empty parentheses are just noise anyway. Unless it would conflict with something else. But the only thing that comes to my mind is if one wants to get the function itself, not call it. But that's what the & operator is for anyway: button.onClick =3D &widget.hide; a. 2010/11/8 Steven Schveighoffer <schveiguy yahoo.com>:On Mon, 08 Nov 2010 14:48:20 -0500, Adam Cig=E1nek <adam.ciganek gmail.co=m>wrote:lyHello, It seems that if function has no parameters, it's possible to omit the parentheses when calling it: =A0 =A0string sayHello() { =A0 =A0 =A0return "hello"; =A0 =A0} =A0 =A0void main() { =A0 =A0 =A0writeln(sayHello); // same as writeln(sayHello()); =A0 =A0} Is this an actual defined (and documented) behaviour that I can expect to keep working in the future, or just some random unintended side effect which might disappear in a next version?It is documented behavior in D1 and originally in D2. =A0D2 will eventual=require you to mark function calls omitting parentheses with property. =A0This is valid D2 today, but the property part is not required to call=thefunction: property string sayHello() { =A0 =A0return "hello"; } D1 will remain the same, i.e. it will not require property. -Steve
Nov 08 2010
On Mon, 08 Nov 2010 15:29:15 -0500, Adam Cigánek <adam.ciganek gmail.com> wrote:Ok, thanks. I knew about property, but not that it works even without it. Actually, is property even needed? I think that the ability to call functions without parentheses could be useful for non-property like function as well: widget.hide; instead of: widget.hide();The point is that parentheses imply a function, where lack-of implies property. For example: if(x.read) Could mean, read something from x, see if it's valid, or check if x has been read. With forcing omitting parentheses or using them, the author of x is able to keep code that uses his object consistent. The only alternative to this is to make the function/property a compound word like 'isRead' or 'readValue', and we are back to Java. For functions that return no value, it's pretty obvious that those are functions and not properties. I have proposed that it might be possible to allow omitting parentheses on parameter-less functions that return void, but I have no idea if that will be implemented.The empty parentheses are just noise anyway. Unless it would conflict with something else. But the only thing that comes to my mind is if one wants to get the function itself, not call it. But that's what the & operator is for anyway: button.onClick = &widget.hide;When the compiler is changed to require omission of parentheses for properties, you will not be able to get the address of the function via the & operator. This is a limitation of the requirement. Otherwise, if the property returns an lvalue, how do you know what the user wants, an address to the lvalue or the function address? I've proposed to work around this using __traits syntax: auto dg = __traits(getDelegate, x.read); Haven't got any feedback on that idea. It could also make getting delegates to specific overloads much more friendly. -Steve
Nov 08 2010