Error: xxx is not an lvalue

Tyro[a.c.edwards] nospam at home.com
Sun May 3 03:55:37 PDT 2009


On 5/3/2009 6:25 PM, Unknown W. Brackets wrote:
> This code works fine (D 2.x only):
>
> class Test
> {
> private int[int] _testMap;
> public ref int[int] testMap() {return _testMap;}
> }
>
> void main()
> {
> Test test = new Test();
> test.testMap[0] = 1;
> }
>
> Note the "ref". Otherwise, a value is returned which is not modifiable.
> This will also fix test.array.length. Nothing wrong here, no creepy bad
> temporary property problems.

Is this an intended or even desirable "feature" of the language or just 
something that happens to work in D 2.x? I ask because to the untrained 
eye, it is unclear exactly what is happening. There is nothing to inform 
me that _testMap can be modified from outside the class. Even with "ref" 
there it doesn't make it any clearer.

Matter of fact, I just had a problem in Phobos 2 where a function was 
defined like that and I had to comment out the "ref" for it to work. 
Kept telling me that some array was not an lvalue. The minute I 
commented out the "ref", everything worked like a charm.

Also it makes more sense to me to have separate functions as getters and 
setters. Maybe that's only because I lacking proper training in the art 
of computer programming though.

> I suppose you could also try returning a pointer to an associative array
> (e.g. with &) in D 1.x.
>
> -[Unknown]
>
> flourish wrote:
>> Hi,
>>
>> why does the following code not compile -- or how to declare a (usable)
>> property of an associative array field?
>>
>> //////////////////////
>> class Test
>> {
>> private int[int] _testMap;
>> public int[int] testMap() {return _testMap;}
>> }
>>
>>
>> void main()
>> {
>> Test test = new Test();
>> test.testMap[0] = 1;
>> }
>> /////////////////
>>
>> *** Error: test.testMap() is not an lvalue
>>
>>
>> Regards,
>> flourish
>>
>>
>>




More information about the Digitalmars-d mailing list