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

ag0aep6g anonymous at example.com
Sat Jun 19 09:43:18 UTC 2021


On 18.06.21 17:44, Dennis wrote:
> So it turns out the double duty of the `return` storage class is neither 
> simple, nor expressive enough. Do you have any ideas how to move 
> forward, and express the `Vector.opIndex` method without making the 
> attribute soup worse? Keep in mind that dip25 (with `return ref`) is 
> already in the language, but dip1000 (with `return scope`) is still 
> behind a preview switch.

A quick and easy fix could be introducing `return(ref)` and 
`return(scope)`, allowing the programmer to pick what `return` binds to. 
Then `opIndex` can be written this way:

----
ref float opIndex(size_t i) return(scope) {
     return this._elements[i];
}
----

But:

* That's still hard to figure out, especially with methods because `ref 
this` is invisible.
* It doesn't address the underlying issues: one level of `scope` is not 
enough, and treating `ref` different from other indirections is confusing.

I'm afraid DIPs 25 and 1000 are falling short.


More information about the Digitalmars-d mailing list