DIP1000: 'return scope' ambiguity and why you can't make opIndex work
Walter Bright
newshound2 at digitalmars.com
Mon Jul 5 09:28:47 UTC 2021
On 6/18/2021 8:44 AM, Dennis wrote:
> Here's the reduced code:
> ```D
> struct Vector {
> float[] _elements;
> ref float opIndex(size_t i) scope return {
> return this._elements[i];
> }
> }
> ```
It *always* helps to reduce these examples to the bare minimum, i.e. strip all
the higher level constructs out. The compiler absolutely must be consistent in
this. The above reduces to:
ref int test(ref scope return int* p)
{
return *p;
}
Take a moment to satisfy yourself that it is logically the same. [] was replaced
by *, float by int, i no longer needed, don't confuse things with operator
overloading, replace `this` with a ref parameter making it a non-member
function, and then strip away the struct wrapper.
This currently compiles with master and -dip1000 without error.
Examining Dennis' table, it has a [ref return type] and a [scope value], and so
the `return` applies to the pointer type.
And so it is working as intended and is not a bug.
I totally understand that this is headache-inducing. The aspirin is realizing
that 100% of these examples can be rewritten using *only* the following terms:
int i;
int* p;
return i;
return *p;
return &i;
return &p;
Write the example using these, sprinkling in ref, return, and scope.
If anything else appears, like class, struct, this, [], delegate, etc., more
work is needed to reduce it.
Hope this helps!
More information about the Digitalmars-d
mailing list