www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - dmd v0.95 properties, error of some sort required

reply Regan Heath <regan netwin.co.nz> writes:
class Foo {
   float value()      { printf("getter\n"); return _value; }
   int value(float f) { printf("setter\n"); _value = f; return 500; }
   float _value = 50;

void main() {
   Foo f = new Foo();
   float b = 100;

   printf("%f %f\n",f.value,b);
   printf("%f\n",f.value = b);

D:\D\src\build\temp>dmd prop.d
d:\d\dmd\bin\..\..\dm\bin\link.exe prop,,,user32+kernel32/noi;

50.000000 100.000000

A friend of mine was trying D for the first time, he expected the setter 
should return success/failure of the set operation and so fashioned a 
setter like the one above. I replied that it should return the value, not 
success or failure so as you could use it in statements like the printf 
above and others eg.

float b,c;

c = f.value = b;

but! it appears that his accidently malformed setter compiles and runs as 
in prop.d above, and does so also with the addition of a line like the one 

Surely the line:
   c = f.value = b;

should generate an error?

Is it ever valid for a property setter to return a different type? I think 
the answer is 'no'. In which case the compiler should fail to compile the 
incorrect setter above.

If it is too hard to tell the setter from a normal function then a 
'property' keyword might be required (after all) to signal the programmers 
intent to the compiler.


Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 11 2004
parent "C. Sauls" <ibisbasenji yahoo.com> writes:
This is precisely the sort of reason why I've always wanted the 
property/get/set blocks, and why I really never use D's properties now 
(with a very few exceptions, like my much-beloved singleton 
implementation, which could improve if TypeInfoClass ever gets a 
constructor delegate attached).  Although, technically, it /can/ be 
useful to have settors return other types, particularly if the "type" of 
the property is fairly transient, such as properties that don't actually 
sit on top of a field, but are conveniant ways of working with states 
and conditions or the like.

-Chris S.
Jul 12 2004