The point of const, scope, and other attributes
Joseph Rushton Wakeling
joseph.wakeling at webdrake.net
Sat May 14 09:34:51 UTC 2022
On Saturday, 14 May 2022 at 01:11:03 UTC, Ali Çehreli wrote:
> So, I can't imagine a use case where lvalue mutation is
> important but rvalue mutation is not.
Maybe for a function that wants to define optional out parameters
that the caller can care about or not? It's a trivial example,
but something like:
```D
import std.stdio: writefln;
void foo () (int input,
auto ref int optional_output = 0 /* dummy rvalue
default */)
{
writefln!"Input received: %s"(input);
optional_output = input * 2;
}
void main()
{
foo(7); // `optional_out` parameter is effectively ignored
writefln("Output parameter was not used");
int output;
foo(19, output); // `optional_out` parameter writes to lvalue
writefln!"Output received: %s"(output);
assert(output == 38);
}
```
A similar pattern might also be useful for passing optional
context object to which a function might write, say, information
on its progress.
Not sure that either of these would be wise, but at least they
are conceivable options.
More information about the Digitalmars-d
mailing list