On Sun, 03 May 2009 01:02:41 +0400, Tyro[a.c.edwards] <[email protected]> wrote:

On 5/3/2009 3:44 AM, 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;}
}


testMap() is a getter and can only return the value it is designed to.
You must implement a setter to accomplish what you are trying to di. Unfortunately I'm not sure how you do that with AAs so I'll leave that to someone that's a little smarter than I.

I think that maybe implementing opIndexAssign might work for you though:

public void opIndexAssign(int val, int ndx){ _testMap[ndx] = val; }

Then you can simply "test[0] = 1;" to get the desired effect.

void main()
{
   Test test = new Test();
   test.testMap[0] = 1;
}
/////////////////

*** Error: test.testMap() is not an lvalue


Regards,
     flourish





No, you are wrong, testMap works like a property here. I believe, original code 
_should_ work. For example, it works if you replace int[int] with int[].
All the trouble is because of an awful properties implementation in D, and 
temporaries not being able to be passed by reference:

struct Foo
{
   private int[] _arr;  
   int[] arr() { return _arr; }
}

void doSomething(int[] a)
{
}

void doSomethingElse(ref int[] a)
{
}

void doSomethingElseConst(ref const(int)[] a)
{
}

void main()
{
   Foo foo;
   foo.arr[0] = 1; // works
   //foo.arr.length = 1; // doesn't

   foo.arr().doSomething(); // works
   //foo.arr.doSomething(); // doesn't

   //foo.arr().doSomethingElse();               // doesn't
   //foo.arr().doSomethingElseConst();  // doesn't
}

Reply via email to