This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Actors and Continuation

5 replies
mailleux
Joined: 2008-08-23,
User offline. Last seen 4 years 7 weeks ago.
I was reading Event-Based Programming without Inversion of Control and testing things on my project and I ran into something that appear to be a bug, and raised some questions in my mind. When I run the following code

      react {
        case 'LOCK =>
          println("I locked!")
          receive { case 'UNLOCK => println("Unlocked!")}
          println("Hey I'm running")
      }

And send:
a ! 'LOCK
a ! 'UNLOCK

I get:

I locked!
Unlocked!
Hey I'm running

What I expected. Now this code:

      react {
        case 'LOCK =>
          println("I locked!")
          react { case 'UNLOCK => println("Unlocked!")}
          println("Hey I'm running") // SHould be Unreachable
      }

I get (again what I expected):

I locked!
Unlocked!

But I don't get any errors on the compiler for the Unreachable line. So if I don't pay attention I may silently lose lines of code. This looks like a bug.

This brings me some other questions, when I use "receive" how does it know to continue with the next line? I looked at the Actor.class but I don't understand how it knows to continue with the next line.

Thomas


Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors and Continuation
On Tue, Dec 30, 2008 at 5:34 AM, Thomas Sant Ana <mailleux [at] gmail [dot] com> wrote:
But I don't get any errors on the compiler for the Unreachable line. So if I don't pay attention I may silently lose lines of code. This looks like a bug.

I don't think it's a bug but it would be a worthwhile improvement.
 
This brings me some other questions, when I use "receive" how does it know to continue with the next line? I looked at the Actor.class but I don't understand how it knows to continue with the next line.

receive() holds the thread and hence holds the complete execution context.

react() does not hold the thread and loses the (thread-associated) execution context because it's based on lightweight closures for continuation support.

alex

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Actors and Continuation

Quoting Alex Boisvert :

> react() does not hold the thread and loses the (thread-associated) execution
> context because it's based on lightweight closures for continuation support.

More specifically, IIUC react() always throws an internal
SuspendActorException (which is caught by the actor framework) after
filing away the PartialFunction you pass it.

I think what you'd want here is some kind of "@alwaysthrows"
annotation that would result in an unreachable code warning. I've
often found myself wanting the same thing in Java when exception
handling gets more reusable.

-0xe1a

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: Actors and Continuation
Scala has the Nothing type which is a promise to diverge.  Don't need an @alwaysthrows annotation.

On Tue, Dec 30, 2008 at 1:30 PM, Alex Cruise <alex [at] cluonflux [dot] com> wrote:
Quoting Alex Boisvert <boisvert [at] intalio [dot] com>:

react() does not hold the thread and loses the (thread-associated) execution
context because it's based on lightweight closures for continuation support.

More specifically, IIUC react() always throws an internal SuspendActorException (which is caught by the actor framework) after filing away the PartialFunction you pass it.

I think what you'd want here is some kind of "@alwaysthrows" annotation that would result in an unreachable code warning.  I've often found myself wanting the same thing in Java when exception handling gets more reusable.

-0xe1a

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.


Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Actors and Continuation

Quoting James Iry :

> Scala has the Nothing type which is a promise to diverge. Don't need an
> @alwaysthrows annotation.

Oh, right! So I guess all we'd need is a compiler enhancement to
detect unreachable code following a Nothing-typed function call.

-0xe1a

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Actors and Continuation

Quoting Alex Cruise :

> Oh, right! So I guess all we'd need is a compiler enhancement to
> detect unreachable code following a Nothing-typed function call.

I went looking in trac and found out about "-Ywarn-dead-code", which
already does this.

-0xe1a

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland