www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: True Properties Poll

reply Jesse Phillips <jessekphillips+d gmail.com> writes:
Nick Sabalausky Wrote:

 "Jesse Phillips" <jessekphillips+d gmail.com> wrote in message 
 news:h4vg5q$13mn$1 digitalmars.com...
 I find writing

 property int foo { get; set;}

 More work than

 public int foo;

That's an unfair apples-to-oranges comparison. This: property int foo { get; set;} Is comparable to this current code: private int _foo; int foo() { return _foo; } int foo(int value) { _foo = value; return value; } It is *not* comparable to: public int foo;

I thought this might come up, but what do you gain from all that extra code? I heard mention of not being able to get the address of the field... but that is a very deliberate thing to do and should not be the purpose of getters/setters.
Jul 31 2009
parent =?UTF-8?B?IkrDqXLDtG1lIE0uIEJlcmdlciI=?= <jeberger free.fr> writes:
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

Jesse Phillips wrote:
 Nick Sabalausky Wrote:
=20
 "Jesse Phillips" <jessekphillips+d gmail.com> wrote in message=20
 news:h4vg5q$13mn$1 digitalmars.com...
 I find writing

 property int foo { get; set;}

 More work than

 public int foo;

This: property int foo { get; set;} Is comparable to this current code: private int _foo; int foo() { return _foo; } int foo(int value) { _foo =3D value; return value; } It is *not* comparable to: public int foo;

I thought this might come up, but what do you gain from all that extra =

=2E but that is a very deliberate thing to do and should not be the purpo= se of getters/setters. Nothing. If you're property is destined to be read from and written=20 to and all it does is access the underlying field, then you might as=20 well use a public field anyway. A "special" property syntax becomes=20 necessary when you want to do one of the following: - Read only access (note that this can't be done with "const" if=20 you want the object to be able to modify its own properties but want=20 to prevent outside code from doing it); - Write only access; - Take extra action on access (i.e mark object "dirty" or notify=20 registered listeners when a property is changed); - "Virtual" properties, i.e properties that don't correspond to=20 any memory storage (or where the storage is merely a cache for a=20 value that's expensive to compute). Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
Jul 31 2009