Jump to page: 1 2
Thread overview
Why isn't the array lenght property an lvalue?
Apr 07, 2014
Jeroen Bollen
Apr 07, 2014
Jeroen Bollen
Apr 07, 2014
MrSmith
Apr 07, 2014
Jeroen Bollen
Apr 07, 2014
MrSmith
Apr 07, 2014
MrSmith
Apr 08, 2014
Iain Buclaw
Apr 07, 2014
Justin Whear
Apr 07, 2014
Jeroen Bollen
Apr 07, 2014
Justin Whear
Apr 07, 2014
Jeroen Bollen
April 07, 2014
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?
April 07, 2014
On Monday, 7 April 2014 at 19:13:31 UTC, 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?

Well type would be ulong, so const(ulong)*
April 07, 2014
On Monday, 7 April 2014 at 19:23:45 UTC, Jeroen Bollen wrote:
> On Monday, 7 April 2014 at 19:13:31 UTC, 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?
>
> Well type would be ulong, so const(ulong)*

Array length is size_t which is uint on x86 and ulong on x86_64.
April 07, 2014
On Monday, 7 April 2014 at 19:56:38 UTC, MrSmith wrote:
> On Monday, 7 April 2014 at 19:23:45 UTC, Jeroen Bollen wrote:
>> On Monday, 7 April 2014 at 19:13:31 UTC, 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?
>>
>> Well type would be ulong, so const(ulong)*
>
> Array length is size_t which is uint on x86 and ulong on x86_64.

Alright, but why can't you get a pointer to it?
April 07, 2014
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.
April 07, 2014
On Monday, 7 April 2014 at 20:12:48 UTC, Justin Whear 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.

Then you still should be able to get a pointer of the result. I was also considering it to be a @property function, as indeed that'd explain this behaviour, but it still doesn't make sense to not allow it. After all, length is just a variable.
April 07, 2014
On Mon, 07 Apr 2014 20:14:13 +0000, Jeroen Bollen wrote:

> On Monday, 7 April 2014 at 20:12:48 UTC, Justin Whear 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.
> 
> Then you still should be able to get a pointer of the result. I was also considering it to be a @property function, as indeed that'd explain this behaviour, but it still doesn't make sense to not allow it. After all, length is just a variable.

Unless the length function returns by ref, the result is an rvalue and you won't be able to take a reference.
April 07, 2014
On Mon, 07 Apr 2014 16:12:48 -0400, Justin Whear <justin@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
April 07, 2014
On Monday, 7 April 2014 at 19:58:21 UTC, Jeroen Bollen wrote:
> On Monday, 7 April 2014 at 19:56:38 UTC, MrSmith wrote:
>> On Monday, 7 April 2014 at 19:23:45 UTC, Jeroen Bollen wrote:
>>> On Monday, 7 April 2014 at 19:13:31 UTC, 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?
>>>
>>> Well type would be ulong, so const(ulong)*
>>
>> Array length is size_t which is uint on x86 and ulong on x86_64.
>
> Alright, but why can't you get a pointer to it?

I've tried, but no luck here
http://dpaste.dzfl.pl/be526902ef4f
April 07, 2014
On 4/7/14, 1:30 PM, MrSmith wrote:
> On Monday, 7 April 2014 at 19:58:21 UTC, Jeroen Bollen wrote:
>> On Monday, 7 April 2014 at 19:56:38 UTC, MrSmith wrote:
>>> On Monday, 7 April 2014 at 19:23:45 UTC, Jeroen Bollen wrote:
>>>> On Monday, 7 April 2014 at 19:13:31 UTC, 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?
>>>>
>>>> Well type would be ulong, so const(ulong)*
>>>
>>> Array length is size_t which is uint on x86 and ulong on x86_64.
>>
>> Alright, but why can't you get a pointer to it?
>
> I've tried, but no luck here
> http://dpaste.dzfl.pl/be526902ef4f

This stuff is best moved to the learn forum. -- Andrei
« First   ‹ Prev
1 2