calling functions without parentheses

Adam Cigánek adam.ciganek at gmail.com
Mon Nov 8 12:29:15 PST 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 = &widget.hide;

a.

2010/11/8 Steven Schveighoffer <schveiguy at yahoo.com>:
> On Mon, 08 Nov 2010 14:48:20 -0500, Adam Cigánek <adam.ciganek at 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
>


More information about the Digitalmars-d-learn mailing list