Multiple return values
The ability to return multiple values from a single method invocation can be quite handy at times. In Java, one must create a new class type whose sole purpose is to hold multiple return values, which is a bit tedious to do. Returning multiple values from one method call is especially useful when calling over an IMOP connection, because using a special data type for return values is much more costly in that the runtime need to make network reflection requests to figure out its definition. Therefore, both the Meso language and the IMOP protocol support methods that return multiple values.
For example, this is an imop: object that returns both high and low temperatures from one method call:
namespace imop:example.com/foo;
public object Weather {
public int, int getTemperatures() {
int high = // read the high temperature
int low = // read the low temperature
return high, low; // return both values
}
}
We can call the method from a client program:
namespace meso:;
import imop:example.com/foo.Weather;
import java:java.lang.System;
public class Client {
public static void main(string[] args) {
int high, low;
high, low = Weather.getTemperatures(); // takes two return values
System.out.println( "High: " + high );
System.out.println( "Low: " + low );
}
}
Multiple return values can be assigned to other method arguments directly. For example, in the following program, the two return values from the getTemperatures() method is used as the arguments for the average() method:
namespace meso:;
import imop:example.com/foo.Weather;
import java:java.lang.System;
public class Client {
public static void main(string[] args) {
double avg = average( Weather.getTemperatures() ); // here
System.out.println( "Average: " + avg );
}
public static double average( imop:int x, imop:int y ) {
return (double) ( x + y ) / 2;
}
}
The types of return values are not part of the method signature. Therefore, two overloaded methods cannot differ only in their return types. For example:
namespace meso:;
public class Client {
public int foo() {
return 10;
}
public int, int foo() {
return 10, 20;
}
}
The Meso compiler will throw this error message out when compiling the class above:
Syntax error in source /Users/stefan/local/Client.meso:
[9,20 - 24] Duplicate method foo() in type meso:Client
