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

Dukc ajieskola at gmail.com
Sat Jun 19 15:47:03 UTC 2021


On Saturday, 19 June 2021 at 15:27:24 UTC, Dennis wrote:
>
> 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.

I'm not sure that `scope ref` should have any different semantics 
from `ref` anyway. If `vector` were passed by value it'd work 
with stack memory. But for that to work with operator overloading 
in same time, we'd need a non-`ref` `this` argument, or 
overloading with `static` or UFCS functions.

Okay, now I'm not sure what would be the best course of action.


More information about the Digitalmars-d mailing list