Re: Multiple return values

tiggr@es.ele.tue.nl (Pieter Schoenmakers)20 Apr 1997 12:11:56 -0400

From comp.compilers

Related articles
[6 earlier articles]
Re: Multiple return values preston@tera.com (1997-04-18)
Re: Multiple return values jbuck@Synopsys.COM (1997-04-18)
Re: Multiple return values smryan@mail.com (1997-04-20)
Re: Multiple return values danwang@dynamic.CS.Princeton.EDU (1997-04-20)
Re: Multiple return values tiggr@es.ele.tue.nl (1997-04-20)
Re: Multiple return values hrubin@stat.purdue.edu (1997-04-20)
Re: Multiple return values fjh@mundook.cs.mu.OZ.AU (1997-04-22)
Re: Multiple return values roy@earthlight.co.nz (1997-04-22)
Re: Multiple return values Robert.Harley@inria.fr (1997-04-22)
Re: Multiple return values jashley@eecs.ukans.edu (Mike Ashley) (1997-04-22)
Re: Multiple return values burley@tweedledumb.cygnus.com (Craig Burley) (1997-04-22)
[17 later articles]
| List of all articles for this month |

 From: tiggr@es.ele.tue.nl (Pieter Schoenmakers) Newsgroups: comp.compilers,comp.lang.misc Date: 20 Apr 1997 12:11:56 -0400 Organization: Eindhoven University of Technology References: 97-04-091 97-04-112 Keywords: syntax, design

I think it is an artefact of the origin of the "function" concept. The
idea of a function in maths is that you give it some parameters and it
performs some calculation on them. The result of the calculation is
the return value. It doesn't make sense to have, for example, an
arctangent return more than one value.

What about a function R x R -> R x R?

[What follows is a general remark in this thread, not specifically a
followup to this posting]. Packing multiple return values into a
struct is not the same as a multiple return. Multiple return means
that a function returns more than one thing, not that it returns a
single thing containing more than one thing. Also, viewing multiple
return values as a single return value plus a bunch of `out' arguments
is dictated by the compiler implementation targeted view of language
designers.

Since a lot of postings in this thread give the workaround to multiple
return values in their favourite language, I tell y'all how it's done
in TOM, a language I'm developing.

In TOM, a method wishing to return multiple values can collect those
values into a tuple (and, indeed a tuple has a type being the tuple type
consisting of the types of its elements):

(int, int)
divmod (int, int) (a, b)
{
return (a / b, a % b);
}

You can see there's something weird with the arguments to the method:
the arguments A and B are packed into a tuple and preceding that is
the tuple type the tuple (A, B) should have. This mechanism of
packing arguments is identical to packing the return values, since, in
fact, TOM method names can consist of multiple parts, like in
Smalltalk and Objective-C. For example, the following is a String
method which searches for the string S within the range (START, LEN):

(int, int)
rangeOfString String s
range (int, int) (start, len);

The value returned is a tuple with the start of the occurence and the
length of the match. The length will be -1 if no match is found. If
I am only interested in whether S is contained in the receiving
object, I can use the following code:

int l;

(, l) = [some_string rangeOfString s range (0, -1)];
if (l != -1)
{
/* Match... */
}

I'm not interested in the first int returned by the method, hence the
empty first element on the lhs of the assignment.

One other possible use of tuples is, of course, simultaneous assignment:

(a, b) = (b, a);

And one final remark: tuples in TOM are not first-class types: the type of
a variable can not be a tuple type.