www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [Question] Could a function return a list of arguments to call another

reply "MattCoder" <mattcoder hotmail.com> writes:
Hi,

I would like to know if it's possible to pass the return of a 
function as argument to another function as below:

import std.stdio;

auto foo(int x, int y){
	writeln(x, y);
	return 3, 4;
}

void main(){
	foo(foo(1,2));
}

I would like to print:
1 2
3 4

PS: I tried return a tuple but it doesn't works.

Thanks,

Matheus.
Jun 28 2013
next sibling parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 06/28/2013 11:07 AM, MattCoder wrote:
 Hi,

 I would like to know if it's possible to pass the return of a function
 as argument to another function as below:

 import std.stdio;

 auto foo(int x, int y){
      writeln(x, y);
      return 3, 4;
 }

 void main(){
      foo(foo(1,2));
 }

 I would like to print:
 1 2
 3 4

 PS: I tried return a tuple but it doesn't works.

 Thanks,

 Matheus.
No, functions cannot return tuples. However, they can return std.typecons.Tuple, so you could do this: auto foo(int i, int j) { writeln(i, " ", j); return tuple(3,4); } void main() { foo(foo(1,2).field); }
Jun 28 2013
next sibling parent "MattCoder" <mattcoder hotmail.com> writes:
On Friday, 28 June 2013 at 19:39:41 UTC, Ellery Newcomer wrote:
 However, they can return std.typecons.Tuple, so you could do 
 this:

 auto foo(int i, int j) {
     writeln(i, " ", j);
     return tuple(3,4);
 }
 void main() {
     foo(foo(1,2).field);
 }
Hi Ellery, Thanks for your help, it works nicely.
Jun 28 2013
prev sibling parent David <d dav1d.de> writes:
 However, they can return std.typecons.Tuple, so you could do this:
 
 auto foo(int i, int j) {
     writeln(i, " ", j);
     return tuple(3,4);
 }
 void main() {
     foo(foo(1,2).field);
 }
 
"field" is deprecated in favor of "expand"
Jun 28 2013
prev sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Fri, 28 Jun 2013 20:07:23 +0200, MattCoder <mattcoder hotmail.com>  
wrote:

 Hi,

 I would like to know if it's possible to pass the return of a function  
 as argument to another function as below:

 import std.stdio;

 auto foo(int x, int y){
 	writeln(x, y);
 	return 3, 4;
 }

 void main(){
 	foo(foo(1,2));
 }

 I would like to print:
 1 2
 3 4

 PS: I tried return a tuple but it doesn't works.

 Thanks,

 Matheus.
Not directly, no. But this should work: import std.stdio; import std.typecons : tuple; auto foo(int x, int y){ writeln(x, y); return tuple(3, 4); } void main(){ foo(foo(1,2).tupleof); } -- Simen
Jun 28 2013
parent "MattCoder" <mattcoder hotmail.com> writes:
On Friday, 28 June 2013 at 19:43:37 UTC, Simen Kjaeraas wrote:
 import std.stdio;
 import std.typecons : tuple;

 auto foo(int x, int y){
 	writeln(x, y);
 	return tuple(3, 4);
 }

 void main(){
 	foo(foo(1,2).tupleof);
 }
Hi Simen, Thanks for your help too, it worked!
Jun 28 2013