Function call without parentheses?

Jonathan M Davis jmdavisProg at gmx.com
Mon May 9 17:04:48 PDT 2011


On 2011-05-09 12:33, Rainer wrote:
> Hi,
> 
> why is it possible to leave out parentheses when you call a function?
> 
> This is pretty error prone :(
> 
> class Dummy {
>     private bool isValid;
> 
>     ...
>     bool isValid() {
>         return this.isValid;
>     }
> }

Originally, any function could be treated as a property function if it had the 
right signature for it. That's primarily functions which return something but 
take nothing (which are treated as getters) and functions which return nothing 
and take one argument (which are treated as setters) - though there are a few 
valide variations. As such, a function such as isValid is treated as a 
property function and can be called without parens. This behavior is rather 
messy and does cause certain problems (such as issues when a getter returns a 
delegate), so @property was introduced.

Per TDPL, any function marked with @property is treated as a property function 
and _must_ be called with the property syntax (so, no parens), and any 
function which is _not_ marked with @property is _not_ a property function and 
_cannot_ use the property syntax (so, it must use parens). dmd has not yet 
fully implemented everything in TDPL - including this. dmd has @property, but 
it's not enforced. The old rules still apply.

Now, some folks have been complaining about this strict enforcement of 
@property and want it to be laxer (enforcing property syntax for property 
functions but not requiring parens for non-property functions), which would 
still allow for isValid to be called without parens. So, it's not entirely 
clear what's going to happen with the enforcement of @property, but I expect 
that strict enforcement will eventually be put in place. Regardless, various 
compiler bugs will need to be addressed before @property can be enforced.

So, for now, you can use property syntax with functions not marked with 
@property, because that's how property syntax was initially implemented.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list