DIP1000: Walter's proposal to resolve ambiguity of ref-return-scope parameters
Nick Treleaven
nick at geany.org
Sat Nov 27 11:36:33 UTC 2021
On Saturday, 27 November 2021 at 03:41:05 UTC, Paul Backus wrote:
> For example, here's a really common error that beginning D
> programmers make:
>
> ```d
> struct S
> {
> private int[] a;
> const int[] getArray() const { return a; }
> }
> ```
...
> With an explicit `this` parameter, there is no room for
> ambiguity:
>
> ```d
> const int[] getArray(const ref this) { return a; }
> ```
>
> The outer `const` can only apply to the return value, and the
> inner `const` can only apply to the `this` reference.
Explicit `this` parameter is OK but more verbose. It also could
be confusing for learners if declaring the `this` parameter is
not required for methods with no `this` attributes. And if it is
required, would that break existing code? I suppose there could
be a rule that all methods of a type must follow the same
convention.
Another solution is to require attributes that apply to `this` to
go after the parameters, not before. That would be less verbose,
but may break code and so would need a long deprecation cycle.
(This was first suggested some years ago).
More information about the Digitalmars-d
mailing list