[e-lang] [Fwd: Lightweight processes in Scala]

Mark Miller erights@REDACTED
Tue Jul 18 22:14:16 CEST 2006


On 7/17/06, David Hopwood wrote:
>Subject: Lightweight processes in Scala
>Date: Mon, 17 Jul 2006 14:59:51 +0700
>From: Nick Linker
>
> Martin Odersky and Philipp Haller did some effort and implemented
> Erlang-like lightweight processes over JVM using Scala language. Their
> lightweight "actors" look like this:
>
> class Counter extends Actor {
>    override def run(): unit = loop(0)
>
>    def loop(value: int): unit = {
>        Console.println("Value: " + value)
>        receive {
>            case Incr() => loop(value + 1)
>            case Value(a) => a ! value; loop(value)
>            case Lock(a) => a ! value
>                            receive { case UnLock(v) => loop(v) }
>            case _ => loop(value)
>        }
>    }
> }

>The article can be found at
>http://lampwww.epfl.ch/~odersky/papers/jmlc06.html.

I've now read the article. I'm puzzled by the title and claims -- they
clearly understand that Erlang does "Event-Based Programming Without
Inversion of Control", and they explicitly state that they are seeking
to get the same effects in Scala. Nevertheless, they seem to suggest
that demonstrating event-based programming without inversion of
control is itself novel. I don't get it.

In any case, in the previous conversation about E-style concurrency in
Erlang, Ulf put his finger on the crucial issue: selective receive.
Scala is clearly trying to emulate Erlang's selective receive style.
For contrast, here's how to do something much like the above example
in E as event-based programming without inversion of control, and
without selective receive. The try-finally below is a hack to avoid
introducing a temporary variable.

------------------------------------------------
#!/usr/bin/env rune

pragma.enable("easy-return")
pragma.disable("explicit-result-guard")

def makeCounter() {
    var value := 0
    var optResolver := null
    def counter {
        to __printOn(out :TextWriter) { out.print(`Value: $value`) }
        to incr() { value := value <- add(1) }
        to getValue() { return value }
        to lock() {
            try {
                return value
            } finally {
                def [p,r] := Ref.promise()
                value := p
                optResolver := r
            }
        }
        to unlock(v) {
            optResolver.resolve(v)
            optResolver := null
        }
    }
    return counter
}

? def counter := makeCounter()
# value: Value: 0

? counter.incr()
? counter.getValue()
# value: 1

? counter.lock()
# value: 1

? counter.incr()
? def x := counter.getValue()
# value: <Promise>

? counter.unlock(33)
? x
# value: 34
-----------------------------------------

-- 
Text by me above is hereby placed in the public domain

    Cheers,
    --MarkM



More information about the erlang-questions mailing list