namgk / ambienttalk

Automatically exported from code.google.com/p/ambienttalk
0 stars 0 forks source link

Multiple Return Values #47

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
In certain cases, a function can return multiple meaningful values, but one 
value is used independently. Here, the programmer has to choose whether to 
return a single value or a table of values. 

When returning a table of values, the caller has to deconstruct the return 
values manually, which can be bothersome if he only want to get hold of the 
first result. Other languages solve this problem to a certain extend by having 
output or reference variables.

This feature request proposes the introduction of a new composite language 
value, akin to a table.  Hence, it can be deconstructed using multi-definition 
or multi-assignment.  However, when assigned to a single variable, only the 
first element is returned. The desired semantics is illustrated using long 
division.

def div              := x /- y;
def [div, mod] := x /- y;

In this case, the long division returns a tuple "<div, mod>". In the first 
line, only the first return value is bound. To obtain the same result in case 
the long division returns a table "[div, mod]", one would have to write.

def [div, _ ]     := x /- y;

Note that in this context "_" does not have a special significance. The above 
code example actually defines a variable named "_".

Original issue reported on code.google.com by smost...@gmail.com on 25 Jan 2011 at 1:07

GoogleCodeExporter commented 8 years ago
Note that while the significance of the problem is low for functions like long 
division, this pattern occurs frequently in the definition of e.g. 
"when:"-based language constructs. 

Ideally, such language constructs return a future, so that one can capture the 
result of executing the code block without resorting to imperative programming.

Similarly, they should return a subscription object which allows canceling the 
subscription, and possibly access other properties of the subscription.

--

The case can also be made that the result of sending asynchronous messages can 
be a tuple, where the first return value is the future, accompanied by an 
object to cancel the message (e.g. if it is Sustained). This was already noted 
in Tom's PhD.

Original comment by smost...@gmail.com on 25 Jan 2011 at 1:11

GoogleCodeExporter commented 8 years ago

Original comment by Botje.linux@gmail.com on 24 May 2013 at 1:49