Function calls

Lutger lutger.blijdestijn at gmail.com
Thu Jan 28 14:23:26 PST 2010


On 01/28/2010 11:05 PM, Andrei Alexandrescu wrote:
> Michiel Helvensteijn wrote:
>> 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.
>
> I agree, but I'll also note that I got contradictory opinions from
> different supporters of @property. So now it looks all the more like a
> judgment call, which is exactly what I was worried in the first place.
>
>> 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.
>
> I found my code to be more readable and just as predictable with the
> current rule. And it's not about two keystrokes, it's about persistent
> and ubiquitous syntactic noise.
>
> Gosh, @property does suck. I was afraid that'd be true.
>
>
> Andrei

Lot's of things are judgement calls, I don't see the problem. @property 
is a tool like everything else, there are many more cases where it's 
pretty clear cut. The ambivalence here is inherent in the code, and only 
brought out by the @property question, not caused by it. Notice there 
was also no consensus of what byLine actually did under the hood.

Also, don't forget these:
- @property allows you to forbid the caller using parenthesis, there is 
no other way in D to achieve this for accessor functions
- so far ignored in this thread: any kind of code inspection 
(reflection, compile time, by IDE) can use the '@property property' for 
various purposes.








More information about the Digitalmars-d mailing list