'scope' confusion

Paul Backus snarwin at gmail.com
Sun May 1 05:05:47 UTC 2022


On Sunday, 1 May 2022 at 04:04:04 UTC, Andy wrote:
> Compiling this code results in an error in `g1` only:
> ```
> a.d(30): Error: cannot take address of `ref return` of 
> `xs.opIndex()` in `@safe` function `g1`
> ```
>
>
> Writing `return &xs.inner[0];` would work .. which is exactly 
> what `opIndex` is supposed to do. The question is how to get 
> the compiler to understand this. How can I write `ArrayWrapper` 
> so that it behaves the same way an array does?

Simplified example:

```d
int** p;

ref int* get() @safe
{
     return *p;
}

int** g1() @safe
{
     return &get(); // error
}
```

Looks like the compiler has a blanket rule against taking the 
address of a `ref` return value if the value's type contains 
indirections.


More information about the Digitalmars-d mailing list