Properties

Bill Baxter wbaxter at gmail.com
Sat Jan 10 19:02:03 PST 2009


On Sun, Jan 11, 2009 at 11:27 AM, dsimcha <dsimcha at yahoo.com> wrote:
> == Quote from Miles (_______ at _______.____)'s article
>> Daniel Keep wrote:
>> > Yes, property syntax can simplify some cases, but this isn't one of them.
>> One good distinction properties and normal functions ought to make is
>> that functions should never be called without ()
>
> Why?  This is really superfluous.  I for one think that code is often more
> readable without the annoying empty parentheses around functions that don't take
> any arguments.  This is especially true for member functions of classes and
> structs, even moreso when chaining them such as foo.bar.baz vs. foo().bar().baz().
>
>> , and properties should
>> never have () unless it has a function type.
>> Current syntax allows crazy things like:
>> ----------
>>       exit = 1;       // it is not an assignment
>>       x = toString = getenv = "PATH"; // creepy, but valid D
>>       if (fork == 1)  // not comparing the value of a variable
>> ----------
>
> So?  The spec is not there to prevent people from doing stupid things (making it
> harder to do stupid things *by accident* is a reasonable goal, but we're all
> consenting adults here).  Anyone who would actually do stuff like this on purpose
> in their code is an idiot and deserves what they get.  One could just as easily do
> some pretty stupid things with operator overloading, casts, templates, you name
> it.  Just because stupid things *can* be done with a feature doesn't mean the
> feature should be removed.

I think the point is not that someone would do these things
deliberately, it's that they might do them inadvertently.  It's not
that hard to end up with property names that sound like verbs or vice
versa.  Then the user of your code guesses that delay = true will be
how to turn the 'delay' property on, and it compiles. But it turns out
it it actually does a delay right then, and was really delay(bool
yieldThread) or something like that, a parameterized action.

All this just means one more thing you have to keep in mind while
you're developing.   You have to keep asking yourself "could somebody
mistake this for a property?" every time you write a function with a
short name.  Or "could someone mistake this for an action?" when you
think you're writing a property.

Here's an issue I haven't seen discussed:  What's the type of property
foo?  If a property can't be treated as a function, then it's type
can't be the same as a function.   It also can't be an int or whatever
the referent type is.  When you do __traits mojo and iterate over the
members of the class, what should it return for a property?  Not a
killer I suppose, but there are probably more issues like that that
would need to be dealt with that are more important than what the
declaration syntax looks like.

--bb



More information about the Digitalmars-d mailing list