array.length++ not an lvalue

Koroskin Denis 2korden at gmail.com
Wed Jun 18 09:29:35 PDT 2008


On Wed, 18 Jun 2008 20:14:27 +0400, Erik Lechak <prochak at netzero.net>  
wrote:

> Koroskin Denis Wrote:
>
>> On Wed, 18 Jun 2008 17:12:19 +0400, Erik Lechak <prochak at 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).



More information about the Digitalmars-d mailing list