at:tutorial:actors
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
at:tutorial:actors [2007/04/01 12:50] – added tvcutsem | at:tutorial:actors [2007/04/01 13:05] – tvcutsem | ||
---|---|---|---|
Line 73: | Line 73: | ||
system.println(" | system.println(" | ||
}; | }; | ||
- | }; | + | }); |
>>nil | >>nil | ||
</ | </ | ||
Line 114: | Line 114: | ||
- it is parameter-passed by-copy rather than by-reference in inter-actor message sends. The copy of the isolate received by the remote actor can only access that actor' | - it is parameter-passed by-copy rather than by-reference in inter-actor message sends. The copy of the isolate received by the remote actor can only access that actor' | ||
- external method definitions on isolates are disallowed. The reason for this is that external method definitions implicitly carry a lexical scope (the scope of their definition). Hence, if an isolate with external methods has to be copied, those scopes would have to be copied as well. Following the rule that objects | - external method definitions on isolates are disallowed. The reason for this is that external method definitions implicitly carry a lexical scope (the scope of their definition). Hence, if an isolate with external methods has to be copied, those scopes would have to be copied as well. Following the rule that objects | ||
+ | |||
+ | Returning to the calculator example, the calculator can now add complex numbers locally and send (a copy of) the resulting complex number back to the customer: | ||
+ | |||
+ | < | ||
+ | > | ||
+ | complexNumber.new(1, | ||
+ | complexNumber.new(2, | ||
+ | object: { | ||
+ | def result(sum) { | ||
+ | system.println(" | ||
+ | }; | ||
+ | }); | ||
+ | >>nil | ||
+ | sum=(3,3) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | A word of warning: isolates are objects that are copied freely between actors. As a result, they should be objects whose actual object identity is of little importance. Usually, the identity of by-copy objects is determined by the value of some of the object' | ||
+ | < | ||
+ | def ==(other) { | ||
+ | (re == other.re).and: | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | It is important to note that an isolate has no access whatsoever to its encompassing scope. The following code results in an exception: | ||
+ | |||
+ | < | ||
+ | >def x := 1; | ||
+ | def adder := isolate: { | ||
+ | def add(n) { x + n }; | ||
+ | }; | ||
+ | adder.add(3) | ||
+ | >> | ||
+ | origin: | ||
+ | at adder.add(3) | ||
+ | </ | ||
+ | |||
+ | Sometimes it is useful to initialize an isolate with the values of lexically visible variables. In that case, AmbientTalk allows the programmer to specify which lexical variables should be //copied into// the isolate itself, such that the isolate has its own, local copy of the variable. Lexical variables that need to be copied like this are specified as formal parameters to the closure passed to the '' | ||
+ | |||
+ | < | ||
+ | >def x := 1; | ||
+ | def adder := isolate: { |x| | ||
+ | def add(n) { x + n }; | ||
+ | }; | ||
+ | adder.add(3) | ||
+ | >>4 | ||
+ | </ | ||
=== Futures === | === Futures === |
at/tutorial/actors.txt · Last modified: 2020/02/05 21:26 by elisag