Is returning void functions inside void functions a feature or an artifact?

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Aug 2 14:51:07 UTC 2021


On Mon, Aug 02, 2021 at 02:31:45PM +0000, Rekel via Digitalmars-d-learn wrote:
> I recently found one can return function calls to void functions,
> though I don't remember any documentation mentioning this even though
> it doesn't seem trivial.

This is intentional, in order to make it easier to write generic code
without always having to special-case functions that don't return
anything. E.g.:

	auto myWrapper(alias func, Args...)(Args args) {
		// Don't have to special case void return.
		return func(args);
	}

	int hooray(int i) { return i+1; }
	void boo(int j) { return; }

	int z = myWrapper!hooray(1);
	myWrapper!boo(2);

Otherwise you'd have to litter your code with redundant special cases:

	auto myWrapper(alias func, Args...)(Args args) {
		static if (is(ReturnType!func == void))
			func(args);
		else
			return func(args); // same thing but different
	}

Allowing `return func(args)` for void functions eliminates the need for
such special cases.


T

-- 
"640K ought to be enough" -- Bill G. (allegedly), 1984. "The Internet is not a primary goal for PC usage" -- Bill G., 1995. "Linux has no impact on Microsoft's strategy" -- Bill G., 1999.


More information about the Digitalmars-d-learn mailing list