Properties
Nick Sabalausky
a at a.a
Sat Jan 10 21:26:35 PST 2009
"Bill Baxter" <wbaxter at gmail.com> wrote in message
news:mailman.378.1231643869.22690.digitalmars-d at puremagic.com...
> On Sun, Jan 11, 2009 at 12:01 PM, Miles <_______ at _______.____> wrote:
>> dsimcha wrote:
>>> I for one think that code is often more
>>> readable without the annoying empty parentheses around functions that
>>> don't take
>>> any arguments.
>>
>> It introduces ambiguities, and unexpected behavior, as I exemplified in
>> the previous message.
>>
>> Parentheses (when following an identifier) has a precise meaning of
>> calling the named function. That is something most modern procedural
>> languages do, it is something programmers are used to.
>>
>> I see no advantage at giving the privilege to no-argument functions of
>> being called without parentheses, while at the same time you lose the
>> ability to distinguish between _calling_ the function from _referring_
>> to it.
>
> Not sure what you mean by that. It's just foo vs &foo in most cases.
> ...Except for some cases like where you're trying to return a delegate.
>
I'd consider those situations with delegates to be reason enough.
> I agree that 'except for' is not very pleasant.
>
>>> This is especially true for member functions of classes and
>>> structs,
>>
>> Aren't these member functions good candidates to be made into real
>> properties?
>>
>> If you want or need to use an identifier without parentheses, you have
>> the option of making it into a property. Very simple.
>
> He may want to retain the ability to pass it as a delegate to some
> other function too.
>
takesADelegate({ return foo.aProperty; });
takesADelegate((int newValue) { foo.aProperty = newValue });
Works with both variables and properties. Which is good, because the whole
point of properties is to be used just like a variable. If you ever feel a
need to use the property's actual setter or getter as a delegate, then
something's been designed wrong.
Also:
void func(ref int x) {...}
func(foo.aProperty);
void func(int* x) {...}
func(&foo.aProperty);
*Should* work with both variables and properties (but might need some
thinking in how to actually pull it off correctly. Someone mentioned C# just
doesn't even bother with it.)
More information about the Digitalmars-d
mailing list