Properties don't behave like variables?

Jonathan M Davis jmdavisProg at gmx.com
Sun May 6 19:19:29 PDT 2012


On Monday, May 07, 2012 04:05:20 Mehrdad wrote:
> Why doesn't this compile?
> 
> @property int foo() { return 1; }
> @property void foo(int v) { }
> 
> void main()
> {
> 	foo |= 2;
> }

Because unfortunately, properties aren't currently set up to be quite as 
advanced as that. Properties read like variables and write like variables, but 
they don't read and write at the same time like variables do.

foo = foo | 2;

would work, because each property usage is translated into a single call to a 
function, but

foo |= 2;

doesn't, because it would require it be translated into

foo = foo | 2;

and then translated into calls to the property functions. I'd definitiely argue 
that it should work that way, but D's properties are simplistic enough that 
they don't currently. There may or may not be an enhancement request for it, 
but if not, then there probably should be.

Another example which is brought up from time to time is

++foo;

It doesn't work either, and for the same reasons.

- Jonathan M Davis


More information about the Digitalmars-d mailing list