Suggestion: properties should be treated as 'virtual members variables'

Steve Horne stephenwantshornenospam100 at aol.com
Wed Sep 6 07:00:43 PDT 2006


On Tue, 05 Sep 2006 22:21:39 +1000, Reiner Pope
<reiner.pope at REMOVE.THIS.gmail.com> wrote:

>While we're discussing this, I'm wondering: what is the reason that D 
>doesn't use explicit properties like C# does?
...
>Walter, is there a reason you decided against such a syntax, or are you 
>simply not aware of it?

If I can throw in a guess, it is sometimes kind of nice that a
property can be treated as a method.

For instance, if you need a delegate for a getter/setter function to
pass to another function, you don't need to write it again - you just
use the existing property.

That said, I'm not sure if the trade-off works. Explicit properties
have been around for a while - not just C# but, IIRC, Delphi,
C++Builder, Visual C++ (even without .NET), Python. They work well
enough. I've not had the experience with the D method to form a clear
opinion yet, but I have my doubts.



On Tue, 05 Sep 2006 16:10:59 +0200, mike <vertex at gmx.at> wrote:

>One could get even further and expand the C#-style syntax:
>
>(1) Get rid of superflous {} characters when there's only one statement.
>
>' property int Val
>' {
>'      get return m_val;
>'      set
>'      {
>'          assert(value < 20);
>'          m_val = value;
>'      }
>' }

One advantage of the D method is just a little bit more flexibility.
With setters, you can decide whether to return the assigned value or
not. That means you can decide whether to support the following...

  var1 = obj.propertyname = var2;

Don't know how important that choice is in D yet, but certainly in C++
I don't always return anything from a custom assignment operator.

-- 
Remove 'wants' and 'nospam' from e-mail.



More information about the Digitalmars-d mailing list