Returning a reference to be manipulated

Chris Katko ckatko at gmail.com
Thu Apr 13 07:05:10 UTC 2023


I'm trying to figure out how to return a reference to something 
that may not be a reference type.

```D
struct stats
{
float[string] data=0;

float ref opIndex(string key)
   {
   return data[key]; // want a ref to a specific element
   }
}

void test()
{
stats foo;
auto x = foo.bar(); // returns some sort of reference
        // data["tacos"] = 0
x++;   // data["tacos"] = 1
}
```

Right now, I'm using pointers which resolves to:

```D
// float* opIndex(string key){...} using pointer
(*s["tacos"])++; // works with pointer, but is strange looking

s["tacos"]++; // preferred syntax or something similar
```



More information about the Digitalmars-d-learn mailing list