Properties are Functions

We currently have two property-related problems to solve for version 2 of the D programming language. One problem is is that having functions callable both without and with empty parenthesis creates ambiguities when it returns a callable type (a delegate, a funciton pointer, or an object with an opCall member). A second problem is that we want properties to be nouns or adjectives, and actions to be verbs. In English a lot of words are both nouns, adjective and verbs, which makes it impractical to distinguish a property from a function by its name alone.

Solving the first problem involves having a way to distinguish functions that can and must be called without parenthesis. Unless we want to force all functions with no parameter to be called without “()”, we must have some kind of flag to tell us wich function expects and does not expect a “()”.

The second problem is more complex. It is about the meaning of a name. Say you have a transform function, you’ll have some expectations about what it does based on the name. So I ask you: is a “transform” function an action (a verb) in the sense that it applies some transformation to an object, or is it a property (a noun or adjective) in the sense that it returns something like an affine transform associated with an object? The semantics of the two are completely different, yet they share the same name.

The real ambiguity here is the English language, were many nouns and adjectives are also verbs. We could switch to a natural language that does not have this problem for function name — say French: “transform” becomes “transformer” (action, verb) or “transformation” (property, noun); “empty” becomes “vider” (action, verb) or “vide” (property, adjective) — but that doesn’t seem like a very practical option.

So, beside switching to a non-English language, we have two other options. First we could write a guideline saying properties (nouns and adjectives) should start with some prefix. I tried it. I couldn’t come with anything sane that works for non-boolean properties. In addition, most people don’t read guidelines, and educating people to write “is”, “has” or a modal verb in front of boolean properties would be a major effort. It works in Objective-C, but the function syntax is very different in Objective-C.

So the only option left, assuming we still want to solve the problem, is to introduce a formal syntax in D to distinguish properties (nouns and adjectives) from actions (verbs). A natural fit for that at the call site is to use the syntax without tailing empty parenthesis for properties, mimicking fields, and use the inherited function syntax from C-derived languages for actions. Choosing the right syntax for property definitions is a greater challenge since we want a simple syntax that helps people make the right choice depending on wether they want a property or an action, this without too much deviation from a regular function either.

As for whether properties should be cheap (not perform anything heavyweight) or not, that’s another debate that could result in a guideline.


This is an adaptation of a message posted on the D newsgroup.


  • © 2003–2024 Michel Fortin.