Last update October 18, 2007

Feature Request List /
Multiple Return Values

rough spillage of thoughts: will need cleanup


functions traditionally return only one value

Historic Solutions

  • create a custom struct, populate the struct in the function
  • use pointers to modify "input" variables directly (now formalised in D with the out keyword)
Background Theory

Ideally a function would look and behave a little like an operator so a basic operation would look like:


   a = b

and the equivalent function:


  a = f(b)

However multiple return values in either of the historic solutions are represented in a non intuitive fashion, example:


  int a;
  someFunction( a , b , c , d );
  someOtherFunction( a );

without reading the source, following pointer trails and consulting documentation it is not immediately obvious what is happening. Here a is being modified by someFunction before being processed by someOtherFunction. This example, with one assignment could have been written as example 2, above, but with multiple values affected this becomes problematic

Proposed Solution

By delimiting multiple variables within brackets one can create an inline implicitly typed struct. In my examples square brackets and commas are used but other combinations could be considered;

example of simple multiple assignment:


   int a,b;
   [a,b] = someFunction(x,y);

the declaration of someFunction would be as follows:


   [int,int] someFunction( int x , int y )
     int c,d;
     return [c,d];

For examples 4. and 5. to peacefully coexist the grouped datatypes assigned and returned need to match. The compiler on encountering such a block would create an implicit struct in place of each (here of the form struct{ int a , int b } and struct{ int c , int d } and assume the responsibility of cross-copying the values.

Types could therefore also be mixed:


   [string, int, char] getUserInfo( int iUserID )
      return [ sUserName , iUserAge , cUserGender ];

Essentially this approach formalises the temporary-struct approach to multiple return values, but with the compiler taking the legwork out of the process.

If a return value was to be ignored for whatever reason the compiler could respect that and not throw an error, example:


   [ sName , , cGender ] = getUserInfo( iID );

Here the programmer is not interested in the age field, an implied NULL is placed between the commas, alternativly and for clarity the NULL could be required and not assumed by the compiler.

(Add : Or maybe, for better clarity, use an OCaml construct like [sName, _, cGender] We could also think of aliasing names for the return, so we could just say : [Name -> sName, Gender -> cGender] )

Muliple assignments can also be made possible, this could therefore also meet the requests for a swap operator:


   [x,y] = [y,x];

Again, the compiler would do the hard work behind the scenes, leaving the programmer free to concentrate on their own creativity.

Javascript 1.7 has an interesting take on multiple return values and assignments as described in the final section here:

Though it does extensively overload the square brackets as per my own examples and it's loosely-typed nature allows these to be cross-treated as arrays

FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: October 18, 2007 22:15 (diff))