User Tools

Site Tools


at:tutorial:actors

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
at:tutorial:actors [2007/04/01 13:05] tvcutsemat:tutorial:actors [2007/04/06 20:00] – added tvcutsem
Line 165: Line 165:
 === Futures === === Futures ===
  
-futures language construct+As you may have noticed previously, asynchronous message sends do not return any value (that is, they return ''nil''). Quite often, the developer is required to work around this lack of return values by means of e.g. explicit customer objects, as shown previously. This, however, leads to less expressive, more difficult to understand code, where the control flow quickly becomes implicit. 
 + 
 +The most well-known language feature to reconcile return values with asynchronous message sends is the notion of a //future//. Futures are objects that represent return values that may not yet have been computed. Once the asynchronously invoked method has completed, the future is replaced with the actual return value, and objects that referred to the future transparently refer to the return value. 
 + 
 +Using futures, it is possible to re-implement the previous example of requesting our calculator actor to add two numbers as follows: 
 + 
 +<code> 
 +def sum := calculator<-add(1,2); 
 +</code> 
 + 
 +Futures are a frequently recurring language feature in concurrent and distributed languages (for example, in ABCL, the actor-based concurrent language). They are also commonly known by the name of //promises// (this is how they are called in the [[http://www.erights.org|E language]] and in Argus). In AmbientTalk, futures are not native to the language. However, because of AmbientTalk's reflective infrastructure, it is possible to build futures on top of the language. The system library shipped with AmbientTalk contains exactly this: a reflective implementation that adds futures to the language kernel. This implementation can be found in the file ''at/lang/futures.at''
 + 
 +To enable futures, it suffices to import the futures module and to enable it, as follows: 
 + 
 +<code> 
 +import /.at.lang.futures; 
 +enableFutures(true); 
 +</code> 
 + 
 +The first statement imports the futures module into the current lexical scope. This enables you as a developer to use some additional language constructs exported by the futures module, as will be explained later. The second statement enables the futures behaviour, causing any asynchronous message send to return a future rather than ''nil''
  
 === Actor Mirrors === === Actor Mirrors ===
at/tutorial/actors.txt · Last modified: 2020/02/05 21:26 by elisag