DIP1000: 'return scope' ambiguity and why you can't make opIndex work
Dennis
dkorpel at gmail.com
Sat Jun 19 15:27:24 UTC 2021
On Saturday, 19 June 2021 at 15:18:20 UTC, Dukc wrote:
> ```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.
It currently does compile. `float[] vector` is assumed to have
infinite lifetime here, so you can return its elements by ref
just fine. You cannot call this `opIndex` on a slice of stack
memory, then your signature has to add the `scope` keyword:
```D
ref float opIndex(return ref scope float[] vector, size_t i){
return vector[i];
}
```
And then the compiler needs to know that the returned float's
lifetime is bound to the stack memory you put in, so then you
want `return scope` semantics (which you can't get here because
there's a `ref` return and a `ref` param.
More information about the Digitalmars-d
mailing list