www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - fun with properties

reply teo <teo.ubuntu yahoo.com> writes:
How can I use properties with increment/decrement and +=/-= operators?

I did following tests (the errors are from dmd v2.052):

class T
{
    private int _x;
     property
    public int x() { return _x; }
}

void main()
{
    int[] a;

    a.length++;    // Error: a.length is not an lvalue

    a.length += 1; // Ok


    auto t = new T();

    t.x++;      // Error: t.x() is not an lvalue

    t.x += 1;   // Error: 't.x' is not a scalar, it is a  property int()
                // Error: incompatible types for ((t.x) += (1)): 
' property int()' and 'int'

    t.x()++;    // Error: t.x() is not an lvalue

    t.x() += 1; // Error: t.x() is not an lvalue
}

Basically I want to change the value of a member variable, which is 
accessed only through a property.
It looks like that is partially possible with the length property of 
dynamic arrays although they probably are implemented in a different way.
Mar 23 2011
parent reply Kai Meyer <kai unixlords.com> writes:
On 03/23/2011 06:48 AM, teo wrote:
 How can I use properties with increment/decrement and +=/-= operators?

 I did following tests (the errors are from dmd v2.052):

 class T
 {
      private int _x;
       property
      public int x() { return _x; }
 }

 void main()
 {
      int[] a;

      a.length++;    // Error: a.length is not an lvalue

      a.length += 1; // Ok


      auto t = new T();

      t.x++;      // Error: t.x() is not an lvalue

      t.x += 1;   // Error: 't.x' is not a scalar, it is a  property int()
                  // Error: incompatible types for ((t.x) += (1)):
 ' property int()' and 'int'

      t.x()++;    // Error: t.x() is not an lvalue

      t.x() += 1; // Error: t.x() is not an lvalue
 }

 Basically I want to change the value of a member variable, which is
 accessed only through a property.
 It looks like that is partially possible with the length property of
 dynamic arrays although they probably are implemented in a different way.
You need a "write" property: property { public int x() { return _x; } // Read property public void x(int x1) { _x = x1; } // Write property } As for the dynamic array length property: void main() { int[] a; ++a.length; // Works a.length += 1; // Works a.length++; // Error: a.length is not an lvalue } I don't get why the error on a.length++ either. I'm curious about the answer.
Mar 23 2011
parent reply teo <teo.ubuntu yahoo.com> writes:
On Wed, 23 Mar 2011 08:28:46 -0600, Kai Meyer wrote:

 On 03/23/2011 06:48 AM, teo wrote:
 How can I use properties with increment/decrement and +=/-= operators?

 I did following tests (the errors are from dmd v2.052):

 class T
 {
      private int _x;
       property
      public int x() { return _x; }
 }

 void main()
 {
      int[] a;


      a.length += 1; // Ok


      auto t = new T();


      t.x += 1;   // Error: 't.x' is not a scalar, it is a  property
      int()
                  // Error: incompatible types for ((t.x) += (1)):
 ' property int()' and 'int'


      t.x() += 1; // Error: t.x() is not an lvalue
 }

 Basically I want to change the value of a member variable, which is
 accessed only through a property.
 It looks like that is partially possible with the length property of
 dynamic arrays although they probably are implemented in a different
 way.
You need a "write" property: property { public int x() { return _x; } // Read property public void x(int x1) { _x = x1; } // Write property }
I've already tried that, but to no avail. I am getting the errors stated above. Can you give me a complete working example please?
Mar 23 2011
parent Kai Meyer <kai unixlords.com> writes:
On 03/23/2011 10:09 AM, teo wrote:
 On Wed, 23 Mar 2011 08:28:46 -0600, Kai Meyer wrote:

 On 03/23/2011 06:48 AM, teo wrote:
 How can I use properties with increment/decrement and +=/-= operators?

 I did following tests (the errors are from dmd v2.052):

 class T
 {
       private int _x;
        property
       public int x() { return _x; }
 }

 void main()
 {
       int[] a;


       a.length += 1; // Ok


       auto t = new T();


       t.x += 1;   // Error: 't.x' is not a scalar, it is a  property
       int()
                   // Error: incompatible types for ((t.x) += (1)):
 ' property int()' and 'int'


       t.x() += 1; // Error: t.x() is not an lvalue
 }

 Basically I want to change the value of a member variable, which is
 accessed only through a property.
 It looks like that is partially possible with the length property of
 dynamic arrays although they probably are implemented in a different
 way.
You need a "write" property: property { public int x() { return _x; } // Read property public void x(int x1) { _x = x1; } // Write property }
I've already tried that, but to no avail. I am getting the errors stated above. Can you give me a complete working example please?
Sorry, you're right. I don't think properties are done, especially given these features don't work as expected.
Mar 23 2011