Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 31, 2006 lvalue array.length error | ||||
---|---|---|---|---|
| ||||
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 |
January 31, 2006 Re: lvalue array.length error | ||||
---|---|---|---|---|
| ||||
Posted in reply to akcom | "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 |
January 31, 2006 Re: lvalue array.length error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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
|
February 01, 2006 Re: lvalue array.length error | ||||
---|---|---|---|---|
| ||||
Posted in reply to akcom | 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; } } |
Copyright © 1999-2021 by the D Language Foundation