digitalmars.D.bugs - dmd v0.95 properties, error of some sort required
- Regan Heath (45/45) Jul 11 2004 --[prop.d]--
- C. Sauls (11/11) Jul 12 2004 This is precisely the sort of reason why I've always wanted the
--[prop.d]--
class Foo {
float value() { printf("getter\n"); return _value; }
int value(float f) { printf("setter\n"); _value = f; return 500; }
private:
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);
printf("%f\n",f.value);
}
D:\D\src\build\temp>dmd prop.d
d:\d\dmd\bin\..\..\dm\bin\link.exe prop,,,user32+kernel32/noi;
D:\D\src\build\temp>prop
getter
50.000000 100.000000
setter
0.000000
getter
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
above.
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.
Regan.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 11 2004
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. -Invironz
Jul 12 2004








"C. Sauls" <ibisbasenji yahoo.com>