How to return a reference to structs?

Paul Backus snarwin at gmail.com
Thu Nov 4 18:31:04 UTC 2021


On Thursday, 4 November 2021 at 18:21:06 UTC, Andrey Zherikov 
wrote:
> On Thursday, 4 November 2021 at 13:03:54 UTC, Paul Backus wrote:
>>
>> Have the lambda return by reference:
>>
>> ```d
>> auto get()
>> {
>>     return idx.map!(ref (i) => a[i]);
>> }
>> ```
>
> Making this example a bit complex: I want `get` to return 
> additional data to the reference. How should I change the 
> lambda then?
> ```d
> auto get()
> {
>     return idx.map!(ref (i) => tuple(a[i], i)); // return ref 
> and index for simplicity
> }
> ```

Since there are no such thing as `ref` variables in D, you can't 
store a reference inside a tuple. So in this case, you will have 
to use a pointer:

```d
auto get()
{
     return idx.map!((i) => tuple(&a[i], i));
}
```


More information about the Digitalmars-d-learn mailing list