[110] in java-interest

home help back first fref pref prev next nref lref last post

multiple return values

daemon@ATHENA.MIT.EDU (Jerry Morrison)
Mon May 22 20:28:21 1995

Date: 22 May 1995 17:20:25 -0800
From: "Jerry Morrison" <jerry.morrison@3do.com>
To: "java-interest <java-interest@java.sun.com>" <java-interest@java.sun.com>

The Java language team asked how Mesa procedures return multiple values.

In short, return a "struct" and use "constructors" and "extractors" to make
it convenient. I'll express this in a C-like syntax but I'm not sure whether
to use () or {} for constructors and extractors. Mesa syntax is more regular
and more readable.

    typedef struct Point { int x; int y; } Point;
    Point point;
    int i, width, height;

    point = {10, 2};   /* constructor */

    {width, height} = point;    /* extractor assigns to width and height in
parallel */

A one-element struct auto-coerces to its element type, but not vice versa.

    typedef struct Prime { int i; } Prime;
    Prime prime = {13};  /* constructor */

     i = prime;  /* auto-coercion */
    {i} = prime;    /* extractor */
    i = prime.i;    /* explicit field selection */

A procedure returns multiple values via constructor
    ... return {cos(angle), sin(angle)};
and the caller gets all the results via extractor
    {x, y} = PolarToRectangular(radius, angle);

In Mesa, every procedure's bundle of arguments is an anonymous struct type,
and calling it involves a constructor. Every procedure returns an anonymous
struct type, and getting the results involves an extractor or auto-coercion
to one element. Mesa also has constructors and extractors that refer to
elements by name instead of position.

Hope this helps.
    Jerry


-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post