property syntax strawman

Jarrett Billingsley jarrett.billingsley at gmail.com
Sun Aug 2 06:08:04 PDT 2009


On Sun, Aug 2, 2009 at 3:43 AM, Walter Bright<newshound1 at digitalmars.com> wrote:
> Having optional parentheses does lead to unresolvable ambiguities. How much
> of a problem that really is is debatable, but let's assume it should be
> resolved. To resolve it, a property must be distinguishable from a regular
> function.
>
> One way is to simply add a "property" attribute keyword:
>
>  property bool empty() { ... }
>  property void empty(bool b) { ... }
>
> The problem is that:
>
> 1. there are a lot of keywords already
> 2. keywords are global things
>
> The alternative is to have a unique syntax for properties. Ideally, the
> syntax should be intuitive and mimic its use. After much fiddling, and based
> on n.g. suggestions, Andrei and I penciled in:
>
>  bool empty { ... }
>  void empty=(bool b) { ... }

The *very* first thing I thought of when I saw this was:

virtual void foo() = 0;

I mean, why do we need the abstract keyword when C++ already has a
perfectly obvious, eminently understandable syntax for it?

Right?

> The only problem is when a declaration but not definition is desired:
>
>  bool empty;
>
> but oops! That defines a field. So we came up with essentially a hack:
>
>  bool empty{}
>
> i.e. the {} means the getter is declared, but defined elsewhere.
>
> What do you think?
>

I think this feels like a desperate grasping-at-straws attempt to not
add a keyword (even though pure, nothrow, shared, __gshared, and
immutable all seem to have made it in without any great fanfare).
Throwing esoteric syntax at it complicates the grammar and the parser,
and you end up with this wad of symbols that doesn't intuitively
indicate that it is a property.  Yes, you can get used to anything.
People get used to Perl, too.

I think it's funny that for a week, Andrei has been arguing against
throwing around new syntax to solve this problem, and that's exactly
what you guys have come up with.  Really, how much more complicated
would this make the parser, compared to adding a new attribute?



More information about the Digitalmars-d mailing list