@property ref foo() { ...} won't work...?

Magnus Lie Hetland magnus at hetland.org
Tue Mar 1 03:25:30 PST 2011


In Andrei's book, as well as in the Phobos source, there are property 
getters that return refs, as in...

  @property ref T front() {
      return _payload[0];
  }

... and code that uses this with simple assignments, such as:

  r.front = foo;

For some reason, I can't get this to work in my code :-/ DMD seems to 
insist that I add a setter as well (i.e., @property T front(T val) { 
return _payload[0] = val; }).

On the other hand, I *do* get

  r.front() = foo;

to work. That works well even without @property, of course; and r.front 
fails even without @property.

Then, again, if I write both the getter (front()) and the setter 
(front(T)), that works even without @property (which, then, seems 
rather redundant?).

So ... I guess I have two questions:

1. What's the point of @property (when it seems I get the same 
functionality without it)?
2. How can I make r.front = foo work, when I only have r.front(), 
returning a ref (which seems like it is used in actual code)?

-- 
Magnus Lie Hetland
http://hetland.org



More information about the Digitalmars-d-learn mailing list