| Thread overview | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 18, 2008 array.length++ not an lvalue | ||||
|---|---|---|---|---|
| ||||
Hello all, I just wanted to start by thanking you for the great responses to my previous questions. I appreciate the knowledgeable and friendly nature of this newsgroup. I ran into my first WTF moment with D. I spent way too much time figuring out why 'array'.length++ was a compiler error (Error: x.length is not an lvalue). This is a newsgroup entry describing the problem: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=39179 Is there a document detailing these "special exceptions" so that I don't have to blindly stumble upon them? If not are there any more that should be documented so that I can add them to my Wiki (http://www.lechak.info/wiki/index.php?title=D_Programming_Language)? For what it's worth, I think 'array'.length++ and 'array'.length += 1 should be valid. If that is not an option, it would be nice if the error message could be changed to something like "Error: x.length is a special property that prohibits the use of the increment (++) and add_and_assign (+=) operators". Thanks, Erik Lechak | ||||
June 18, 2008 Re: array.length++ not an lvalue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Erik Lechak | Erik Lechak wrote:
...
> For what it's worth, I think 'array'.length++ and 'array'.length += 1 should be valid. If that is not an option, it would be nice if the error message could be changed to something like "Error: x.length is a special property that prohibits the use of the increment (++) and add_and_assign (+=) operators".
>
> Thanks,
> Erik Lechak
All properties behave like that, it's mentioned in the spec:
"Note: Properties currently cannot be the lvalue of an op=, ++, or -- operator."
In between the spec and the wiki4d, everything can be found but I'm not aware of any site that has a list of common pitfalls specifically, except yours of course...
| |||
June 18, 2008 Re: array.length++ not an lvalue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Erik Lechak | On Wed, 18 Jun 2008 17:12:19 +0400, Erik Lechak <prochak@netzero.net> wrote:
> Hello all,
>
> I just wanted to start by thanking you for the great responses to my previous questions. I appreciate the knowledgeable and friendly nature of this newsgroup.
>
> I ran into my first WTF moment with D. I spent way too much time figuring out why 'array'.length++ was a compiler error (Error: x.length is not an lvalue).
>
> This is a newsgroup entry describing the problem:
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=39179
>
> Is there a document detailing these "special exceptions" so that I don't have to blindly stumble upon them? If not are there any more that should be documented so that I can add them to my Wiki (http://www.lechak.info/wiki/index.php?title=D_Programming_Language)?
>
> For what it's worth, I think 'array'.length++ and 'array'.length += 1 should be valid. If that is not an option, it would be nice if the error message could be changed to something like "Error: x.length is a special property that prohibits the use of the increment (++) and add_and_assign (+=) operators".
>
> Thanks,
> Erik Lechak
>
Length property of the array is not a member variable, but rather a member function:
int[] array = new int[42];
assert(array.length() == 42);
but you can also omit paranthesis (that's merely a syntax sugar):
assert(array.length == 42);
The same way the following:
array.length = 100
is translated into a function call: array.length(100); which does array resizing.
Note that you should normally call array.length(array.length() + 1) (or array.length = array.length + 1) in order to increase array size by 1,
and what you write is this:
array.length() += 1;
array.length()++;
It doesn't make much sense, since you increment a temporary variable!
D *could* do the following trick:
array.length()+=1 -> array.length(array.length() + 1);
but it would break too much of the existing code and makes a little of sense.
Other solution would be to return some object that would behave like int (be opImplicitCast'able to it) and have an overloaded opAssign, opAddAssign, etc, but does anybody *really* interested in it?
P.S. I just checked and it appears that you don't have an option of having paranthesis, unlike usual member functions, i.e. the following is not semantically correct (although it ought to be, IMO):
// trying to resize an array
array.length(42); // error, use array.length = 42 instead.
But the logic is the same. Please, correct me if I'm wrong.
| |||
June 18, 2008 Re: array.length++ not an lvalue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | "Koroskin Denis" <2korden@gmail.com> wrote in message news:op.ucx7xdubenyajd@proton.creatstudio.intranet... > D *could* do the following trick: > array.length()+=1 -> array.length(array.length() + 1); > > but it would break too much of the existing code and makes a little of sense. It makes perfectly good sense. Good enough that most languages that have built-in properties translate augmenatation assignments into exactly what you've written. | |||
June 18, 2008 Re: array.length++ not an lvalue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | On 2008-06-18 16:30:39 +0200, "Koroskin Denis" <2korden@gmail.com> said:
> [some nice points]
> D *could* do the following trick:
> array.length()+=1 -> array.length(array.length() + 1);
>
> but it would break too much of the existing code and makes a little of sense.
actually I think that in some cases for properties it makes sense (for example for my slices of multidimensional arrays) and does not break code *but* normally it defeats the performance reason to do +=.
In fact it is a good idea not to hide things that have a speed penalty, and the extra typing is minimal.
In the case of the length property this is very much so.
Arrays are not the correct data structure for step by step resizes... either try to have the correct size from the beginning, or switch datastructure.
| |||
June 18, 2008 Re: array.length++ not an lvalue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Fawzi Mohamed | On Wed, 18 Jun 2008 19:11:39 +0400, Fawzi Mohamed <fmohamed@mac.com> wrote:
> On 2008-06-18 16:30:39 +0200, "Koroskin Denis" <2korden@gmail.com> said:
>
> In the case of the length property this is very much so.
> Arrays are not the correct data structure for step by step resizes... either try to have the correct size from the beginning, or switch datastructure.
>
D arrays only grow exponentially, so no worries here (although it does consume some time to understand if the capacity is large enough to avoid resizing).
| |||
June 18, 2008 Re: array.length++ not an lvalue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Erik Lechak | Erik Lechak wrote: > Hello all, > > Is there a document detailing these "special exceptions" so that I don't have to blindly stumble upon them? If not are there any more that should be documented so that I can add them to my Wiki (http://www.lechak.info/wiki/index.php?title=D_Programming_Language)? This might be useful. http://prowiki.org/wiki4d/wiki.cgi?ShortFrequentAnswers > > Thanks, > Erik Lechak > No problem. | |||
June 18, 2008 Re: array.length++ not an lvalue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | Koroskin Denis Wrote: > On Wed, 18 Jun 2008 17:12:19 +0400, Erik Lechak <prochak@netzero.net> wrote: > > > Hello all, > > > > I just wanted to start by thanking you for the great responses to my previous questions. I appreciate the knowledgeable and friendly nature of this newsgroup. > > > > I ran into my first WTF moment with D. I spent way too much time figuring out why 'array'.length++ was a compiler error (Error: x.length is not an lvalue). > > > > This is a newsgroup entry describing the problem: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=39179 > > > > Is there a document detailing these "special exceptions" so that I don't have to blindly stumble upon them? If not are there any more that should be documented so that I can add them to my Wiki (http://www.lechak.info/wiki/index.php?title=D_Programming_Language)? > > > > For what it's worth, I think 'array'.length++ and 'array'.length += 1 should be valid. If that is not an option, it would be nice if the error message could be changed to something like "Error: x.length is a special property that prohibits the use of the increment (++) and add_and_assign (+=) operators". > > > > Thanks, > > Erik Lechak > > > > Length property of the array is not a member variable, but rather a member function: > When I compile this: import std.stdio; void main(){ int x[23]; writefln(x.length()); } I get this: Error: undefined identifier length Error: function expected before (), not length of type int So the compiler is telling me length is of type int. > int[] array = new int[42]; > assert(array.length() == 42); This does not compile. > but you can also omit paranthesis (that's merely a syntax sugar): It's not syntactic sugar if it does not compile with the parens. > assert(array.length == 42); Yep, this works, but it does not show that array.length is a method/function. > The same way the following: > array.length = 100 > > is translated into a function call: array.length(100); which does array resizing. Ok. Maybe it gets translated into a method call. But that does not mean that array.length is a method. It sounds more like a tied variable in perl. > Note that you should normally call array.length(array.length() + 1) I would love to do that because it makes sense. But it does not compile. > D *could* do the following trick: > array.length()+=1 -> array.length(array.length() + 1); array.length(array.length() + 1) is not a trick. It is logical. > but it would break too much of the existing code and makes a little of sense. It would break code but it would make sense at least to me:) So I either have to look at properties as methods that are called without parens or integers that restrict my use of operators. That's a bummer. Thanks, Erik Lechak | |||
June 18, 2008 Re: array.length++ not an lvalue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Erik Lechak | This could work if we had real properties. I'd really like to see an extended/modified version of the C# property syntax in D:
private int _length;
public property int length
{
opSet() { return _length = value; }
opGet() { return value; }
opPostIncrement() { _length++; }
// ...
}
That would solve the problem.
-Mike
Erik Lechak Wrote:
> Hello all,
>
> I just wanted to start by thanking you for the great responses to my previous questions. I appreciate the knowledgeable and friendly nature of this newsgroup.
>
> I ran into my first WTF moment with D. I spent way too much time figuring out why 'array'.length++ was a compiler error (Error: x.length is not an lvalue).
>
> This is a newsgroup entry describing the problem: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=39179
>
> Is there a document detailing these "special exceptions" so that I don't have to blindly stumble upon them? If not are there any more that should be documented so that I can add them to my Wiki (http://www.lechak.info/wiki/index.php?title=D_Programming_Language)?
>
> For what it's worth, I think 'array'.length++ and 'array'.length += 1 should be valid. If that is not an option, it would be nice if the error message could be changed to something like "Error: x.length is a special property that prohibits the use of the increment (++) and add_and_assign (+=) operators".
>
> Thanks,
> Erik Lechak
>
| |||
June 18, 2008 Re: array.length++ not an lvalue | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Erik Lechak | On Wed, 18 Jun 2008 20:14:27 +0400, Erik Lechak <prochak@netzero.net> wrote:
> Koroskin Denis Wrote:
>
>> On Wed, 18 Jun 2008 17:12:19 +0400, Erik Lechak <prochak@netzero.net>
>> wrote:
>>
>> > Hello all,
>> >
>> > I just wanted to start by thanking you for the great responses to my
>> > previous questions. I appreciate the knowledgeable and friendly
>> nature
>> > of this newsgroup.
>> >
>> > I ran into my first WTF moment with D. I spent way too much time
>> > figuring out why 'array'.length++ was a compiler error (Error:
>> x.length
>> > is not an lvalue).
>> >
>> > This is a newsgroup entry describing the problem:
>> >
>> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=39179
>> >
>> > Is there a document detailing these "special exceptions" so that I
>> don't
>> > have to blindly stumble upon them? If not are there any more that
>> > should be documented so that I can add them to my Wiki
>> > (http://www.lechak.info/wiki/index.php?title=D_Programming_Language)?
>> >
>> > For what it's worth, I think 'array'.length++ and 'array'.length += 1
>> > should be valid. If that is not an option, it would be nice if the
>> > error message could be changed to something like "Error: x.length is a
>> > special property that prohibits the use of the increment (++) and
>> > add_and_assign (+=) operators".
>> >
>> > Thanks,
>> > Erik Lechak
>> >
>>
>> Length property of the array is not a member variable, but rather a member
>> function:
>>
>
> When I compile this:
> import std.stdio;
> void main(){
> int x[23];
> writefln(x.length());
> }
>
> I get this:
> Error: undefined identifier length
> Error: function expected before (), not length of type int
>
> So the compiler is telling me length is of type int.
>
>> int[] array = new int[42];
>> assert(array.length() == 42);
>
> This does not compile.
>
>> but you can also omit paranthesis (that's merely a syntax sugar):
>
> It's not syntactic sugar if it does not compile with the parens.
>
>> assert(array.length == 42);
>
> Yep, this works, but it does not show that array.length is a method/function.
>
>> The same way the following:
>> array.length = 100
>>
>> is translated into a function call: array.length(100); which does array
>> resizing.
>
> Ok. Maybe it gets translated into a method call. But that does not mean that array.length is a method. It sounds more like a tied variable in perl.
>
>> Note that you should normally call array.length(array.length() + 1)
>
> I would love to do that because it makes sense. But it does not compile.
>
>> D *could* do the following trick:
>> array.length()+=1 -> array.length(array.length() + 1);
>
> array.length(array.length() + 1) is not a trick. It is logical.
>
>> but it would break too much of the existing code and makes a little of
>> sense.
>
> It would break code but it would make sense at least to me:)
> So I either have to look at properties as methods that are called without parens or integers that restrict my use of operators. That's a bummer.
>
> Thanks,
> Erik Lechak
>
>
You missed the P.S. section. Actually, I knew it doesn't compile while posting, but posted anyway because even though it doesn't compile, it is subject to the same rule. Array's `length` is a built-in property, not an int. Can an array capacity increase upon integer assignment :) Properties are methods. Methods can be called with or without parens (except bult-in ones, like length, stringof, etc. Those are called without).
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply