On Saturday, 27 November 2021 at 01:17:15 UTC, zjh wrote:
> On Friday, 26 November 2021 at 14:48:21 UTC, Paul Backus wrote:
> > inout int[] getSomething(inout return this, int offset) @property { ... }
Why copy rust
.
Actually, I got the idea from Python. :)
Why do I think it's a good idea? Because it makes it really obvious which attributes apply to this
and which attributes apply to the function as a whole.
For example, here's a really common error that beginning D programmers make:
struct S
{
private int[] a;
const int[] getArray() const { return a; }
}
If you try to compile this code, you get the following error:
Error: redundant attribute `const`
The reason for this is that in the above code, both instances of const
are treated as applying to the this
reference, not to the return value. The fix is to add ()
to the first const
:
const(int[]) getArray() const { return a; }
With an explicit this
parameter, there is no room for ambiguity:
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.