Talk:Property Discussion Wrap-up

From D Wiki
Revision as of 21:37, 25 January 2013 by RobT (talk | contribs) (ref returns and taking the address: new section)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

ref returns and taking the address

ref T prop()

The current behavior without @property enforcement is that the setter function is called if there is one, else the reference to the return type is used.

prop = 4; // calls setter

If there is a setter property, but you want to bypass the setter, then

prop() = 4; // direct access to T ref

To limit @property ref retuns is a very harsh restriction to impose.

A solution is to allow

ref const(T) prop()

prop = 4; // calls setter

prop() = 4; // compile error

*(prop()) = 4; // compile error

What if you take the address of prop?

&prop returns the getter function address, but I'm not sure how to get the setter prop address.

&prop() returns the ref storage address. If return is const, you should be OK unless you do unsafe operations.

With @property enforcement, if we try to emulate storage addresses, then we'll have to disallow taking the address but that will significantly defeat the purpose of variable emulation. We can do something like this:

&prop returns the address of the setter function if there is one, else compiler error.

*(&prop) = 5; // calls the setter

Is the complexity of true variable emulation justified, or do we implement only partial variable emulation? But is enforcing variable emulation justified if we're to only partially implement? I would say we must either do full variable emulation, or not bother with it and treat the properties as regular functions.

RobT (talk) 22:37, 25 January 2013 (CET)