The experimental feature of Scala described in this document allows to define simultaneously a set of functions (or methods) by giving a set of rules that specify synchronization patterns like in the join-calculus.
It is implemented as an extra phase of the compiler (between the
parser and the typechecker) that transforms join definitions into
normal definitions of functions that use Scala concurrent primitives
(inherited from Java) to synchronize together. This phase is enabled
by giving the option -join
to the Scala compiler
socos.
Note that the presented feature is NOT part of the current version of the Scala compiler.
class Reference[a](init: a) with { module join with { def get: a; def set(x: a): Unit; private def state(x: a): nil; get & state(x) = spawn < (reply (x) to get) | state(x) >; set(x) & state(y) = spawn < (reply to set) | state(x) >; } state(init); }
module join with { ... }
. It is a fixed construct,
i.e. join is a new Scala keyword when the join phase is
enabled.nil
. state
is used to keep the
value. It is declared private because it is not part of the
interface.spawn< e1 | ... | en >
is not a new
construct. It corresponds to the call of the function
spawn defined in the module concurrency and is
used to execute several threads (any Scala expression of type
Unit
) in parallel. reply (e) to f
is used to give a
result (the result of the evaluation of expression e to
the function f. reply to f
is just
syntactic sugar for reply (()) to f
. state(init)
is located outside
the join definition. It is used to store the initial value in
the reference and shows that methods (or functions) defined in a
join definition are used as any other Scala function.
After translation each function defined in the block "module
join with { ... }" becomes a method of the enclosing class or
module. So it is completely valid to call state inside
the class, even if it was declared private.
join { ... }
instead of module join with { ... }
.Join-patterns are the concurrency primitives of the join-calculus. They were the basis of Funnel, a small programming language based on Functional Nets which can be seen as a uniform foundation for programming based on join-calculus. Join-patterns in Scala are used quite the same way there were used in Funnel, namely channels defined in a join-definition are seen as functions or methods of an object. The calls to these methods are synchronized according to the join-pattern.
The compilation of join-patterns made in this implementation is based on the one for jocaml described in the following paper: Compiling Join-Patterns, by Fabrice Le Fessant and Luc Maranget, HLCL'98.