www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Returning multiple values from a function without Tuple

reply tcak <tcak gmail.com> writes:
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.
Sep 13 2020
parent Paul Backus <snarwin gmail.com> writes:
On Sunday, 13 September 2020 at 10:26:21 UTC, tcak wrote:
 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.
Isn't this exactly what Tuple already does? The only difference is that the struct is generated by a template in the standard library, and not by the compiler.
Sep 13 2020