Function calls

Michiel Helvensteijn m.helvensteijn.remove at gmail.com
Thu Jan 28 13:55:51 PST 2010


Andrei Alexandrescu wrote:

>>> foreach (line; stdin.byLine()) { ... }
>>>
>>> vs.
>>>
>>> foreach (line; stdin.byLine) { ... }
>>>
>>> How do I choose?
>> 
>> byLine is a property.  It is fetching a range on stdin.
>> 
>> -Steve
> 
> Damn. I was sure the answer will be different.

byLine() is a function. It changes the state of stdin. Calling it
consecutively will in general result in different return values. If there
were two guys: stdin.currentLine (property) and stdin.nextLine(), it would
be a different story.

A property should act very much like a field. The advantages of properties
are that they can be derived (not have a one-to-one relation with a storage
location) or can do some clever caching/memoizing/logging.

Omitting parentheses on an action just because you can is stupid. It
actually makes the language less readable and less predictable. And all
that for saving two keystrokes.

-- 
Michiel Helvensteijn




More information about the Digitalmars-d mailing list