property syntax strawman

Michiel Helvensteijn m.helvensteijn.remove at gmail.com
Sun Aug 2 05:33:02 PDT 2009


Michiel Helvensteijn wrote:

> --------------------------------------------------
> bool empty {
>     void set(auto value) { ... }
>     auto get() { ... }
> }
> 
> empty = false; // empty.set(false)
> auto b = empty; // auto b = empty.get()
> --------------------------------------------------
> 
> for example, requires no hacks and no keywords. And has the added
> advantage that you can still use the getter and setter methods directly.

It should be noted that there is an ambiguity if the type of the property is
a struct/class with members named 'get' and 'set'. As I see it, there are
three ways to resolve it:

* Disallow properties with such a struct/class. This seems a little bit
excessive and silly. But it is one way.

* The 'set' and 'get' from the property are only used for the
property-calling syntax. They can not be referenced directly. The members
of the struct/class would all be accessable. This would work about the same
way as, for example, the suggestion from John C in this thread. And if you
go down this path, you might as well condense the syntax, as he did.

* The 'set' and 'get' from the property are accessable functions and they
overshadow possible member functions with those names.

My language uses the third way, but there is no problem with overshadowing.
The 'set' and 'get' functions have the same special purpose for every type,
and the property is meant to override them.

I guess D could do something similar using, instead of my 'get' and 'set',
D's 'this' and 'opAssign' respectively.

-- 
Michiel Helvensteijn




More information about the Digitalmars-d mailing list