DIP1000: 'return scope' ambiguity and why you can't make opIndex work

Dukc ajieskola at gmail.com
Sat Jun 19 15:18:20 UTC 2021


On Saturday, 19 June 2021 at 14:48:45 UTC, ag0aep6g wrote:
>
> I think you misunderstand. `return` does bind to `this`. But it 
> can bind to two different aspects of `this`: (1) `ref` or (2) 
> `scope`.
>
> Currently, you cannot freely choose which aspect of `this` you 
> want to be `return`. The `opIndex` example fails because 
> `return` is applied to the wrong aspect, and the programmer 
> can't override it.

Ah thanks, let's try again. So the member function rewritten 
would look like

```d
ref float opIndex(return ref float[] vector, size_t i){
    return vector[i];
}
```

So the problem is that `return` allows returning the vector array 
itself by reference, but not any of it's elements.

I think this function should compile, `return` or no. DIP1000 is 
not supposed to be transitive, so it should only check returning 
`vector` itself by reference, but not anything that it refers to.

However, the user still may want to annotate the `vector` 
parameter as `return`, because that lets the client code to 
ensure that the reference to `vector[i]` won't live longer than 
`vector`.


More information about the Digitalmars-d mailing list