The point of const, scope, and other attributes

H. S. Teoh hsteoh at quickfur.ath.cx
Sat May 14 09:45:33 UTC 2022


On Sat, May 14, 2022 at 09:34:51AM +0000, Joseph Rushton Wakeling via Digitalmars-d wrote:
> 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 */)
> {
[...]
> }
> ```
> 
> A similar pattern might also be useful for passing optional context
> object to which a function might write, say, information on its
> progress.

TBH, for cases like these, I'd just use a pointer and check for null:

	void foo()(int input, int* optional_output = null) {
		...
		if (optional_output !is null)
			*optional_output = ...;
		...
	}

No need to fear pointers, they're in the language for a reason.


T

-- 
Build a man a fire, and he is warm for a night. Set a man on fire, and he is warm for the rest of his life.


More information about the Digitalmars-d mailing list