www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - lvalue array.length error

reply akcom <CppCoder gmail.com> writes:
If this has already been reported, I sincerely apologize;

class Foo
{
	int []arr;
	this()
	{
		arr = new int[12];
	}
	void Bar()
	{
		arr.length++;
		/+
		int len;
		len = arr.length;
		arr.length = len+1;
		+/
	}
}

in Foo.Bar(), the arr.length++ statement raises the following error in 
the compiler: "test.d(126): (this.arr).length is not an lvalue", but 
when using the commented code, everything compiles just fine
Jan 30 2006
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"akcom" <CppCoder gmail.com> wrote in message 
news:drmdtk$1j7u$1 digitaldaemon.com...
 in Foo.Bar(), the arr.length++ statement raises the following error in the 
 compiler: "test.d(126): (this.arr).length is not an lvalue", but when 
 using the commented code, everything compiles just fine
At the very bottom of the "Properties" topic in the D spec, is this notice: Note: Properties currently cannot be the lvalue of an op=, ++, or -- operator. This is because properties are treated like functions. So arr.length++ Would be like writing arr.length()++ Which doesn't make sense (it's a no-op). Instead, you can write arr.length = arr.length + 1
Jan 30 2006
parent akcom <CppCoder gmail.com> writes:
Jarrett Billingsley wrote:
 "akcom" <CppCoder gmail.com> wrote in message 
 news:drmdtk$1j7u$1 digitaldaemon.com...
 in Foo.Bar(), the arr.length++ statement raises the following error in the 
 compiler: "test.d(126): (this.arr).length is not an lvalue", but when 
 using the commented code, everything compiles just fine
At the very bottom of the "Properties" topic in the D spec, is this notice: Note: Properties currently cannot be the lvalue of an op=, ++, or -- operator. This is because properties are treated like functions. So arr.length++ Would be like writing arr.length()++ Which doesn't make sense (it's a no-op). Instead, you can write arr.length = arr.length + 1
ah, my mistake, thank you very much
Jan 30 2006
prev sibling parent Dave <Dave_member pathlink.com> writes:
akcom wrote:
 If this has already been reported, I sincerely apologize;
 
 class Foo
 {
     int []arr;
     this()
     {
         arr = new int[12];
     }
     void Bar()
     {
         arr.length++;
         /+
         int len;
         len = arr.length;
         arr.length = len+1;
         +/
     }
 }
 
 in Foo.Bar(), the arr.length++ statement raises the following error in 
 the compiler: "test.d(126): (this.arr).length is not an lvalue", but 
 when using the commented code, everything compiles just fine
arr.length = arr.length + 1; // also legal That is by design, and I think the general feeling is that the current semantics are correct because .length is not an lvalue - it's a property - so the prohibition against pre/post increment/decrement operators makes sense. Take a look at the bottom of: http://digitalmars.com/d/property.html It's also consistent with this: import std.stdio; void main() { C c = new C; // c.i++; // Error c.i = c.i + 1; // Ok writefln(c.i); } class C { private int _i; int i() { return _i; } int i(int val) { return _i = val; } }
Feb 01 2006