Returning multiple values from a function without Tuple

tcak tcak at gmail.com
Sun Sep 13 10:26:21 UTC 2020


I made some research about returning multiple values from a 
function as it is done in dynamic languages, and there is no 
clear was as far as I can see.

So an idea came to me, and I would like to share it with you. 
Maybe this could be turned into a proposal.

Let's say I have defined a function and its return type is set to 
"auto".

	auto test()
	{
		return struct(
			name = "Adam",
			country: string = "Denmark",
			age: ubyte = 37
		);
	}

Compiler would define an automatically generated struct into code 
that exactly fits to the returned struct. When I call the above 
function as below,

	auto info = test();

Type of the variable "info" is going to be that automatically 
generated struct, and values of the returned struct are going to 
be copied into it.

The only thing that is different in this approach compared to 
returning a normal struct variable is that there is no need to 
define a struct type explicitly and it is done by the compiler, 
the rest is same, but with much cleaner syntax.


More information about the Digitalmars-d mailing list