Why isn't the array lenght property an lvalue?

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 7 13:29:42 PDT 2014


On Mon, 07 Apr 2014 16:12:48 -0400, Justin Whear  
<justin at economicmodeling.com> wrote:

> On Mon, 07 Apr 2014 19:13:30 +0000, Jeroen Bollen wrote:
>
>> When I have myarray.length, why isn't that considered an lvalue,
>> and as a result, why cannot I get a pointer to it?
>>
>> It seems kinda dumb, I understand it cannot be changed manually,
>> but surely you should be able to get a const(type)* from it?
>
> I believe `length` is implemented as a property function, though I can't
> find the source for the life of me.

It's a compiler builtin property. Reading simply returns the value from  
inside the array (does not map to a property). Setting calls one of these  
two depending on the array type:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/lifetime.d#L1282
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/lifetime.d#L1460

The reason you can't get a pointer to it is that it's not a simple  
variable.

Note, unlike C++, D does not allow you to get a const reference or pointer  
to an Rvalue.

-Steve


More information about the Digitalmars-d mailing list