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

Actors and strong typing

8 replies
Chris Lambrou
Joined: 2009-01-11,
User offline. Last seen 42 years 45 weeks ago.
I'm fairly new to Scala, and very much like what I've encountered so far. However, I'm puzzled by the apparent lack of type-safety when dealing with actors. Almost everything seems to be type-checked by the Scala compiler, except when it comes to actors. It appears that anything can be sent as a message to an actor, and the actor then fails at runtime if it's unable to handle the message. Is this indeed the case, or am I missing something? Is there a way to describe the type of messages that an actor will accept, and have this verified by the compiler? If not, are there any useful guidelines to follow to help avoid unwanted messages being passed to actors?

Chris
Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors and strong typing
On Wed, Mar 18, 2009 at 2:24 PM, Chris Lambrou <chris@lambrou.net> wrote:
I'm fairly new to Scala, and very much like what I've encountered so far. However, I'm puzzled by the apparent lack of type-safety when dealing with actors. Almost everything seems to be type-checked by the Scala compiler, except when it comes to actors. It appears that anything can be sent as a message to an actor, and the actor then fails at runtime if it's unable to handle the message. Is this indeed the case, or am I missing something? Is there a way to describe the type of messages that an actor will accept, and have this verified by the compiler? If not, are there any useful guidelines to follow to help avoid unwanted messages being passed to actors?

Hi Chris,

2 quick comments,

1) you can easily avoid failure in your actor by having a catch-all case clause, e.g.

react {
  case Foo => ...
  case Bar => ...
  case _ @ msg => Logger error "Unexpected message: %s".format(msg)
}

2) you can also easily provide a typed interface to your clients, e.g.,

trait SomeTypedActor {
  protected val actor: OutputChannel[Any]

  def foo(x: X) { actor ! Foo(x) }
  def bar(x: X): Y = { actor !? Bar(x) }
}

I imagine somebody will eventually come up with a more convenient and general solution for the above using a little bit of reflection and proxy magic.

alex
 
Szymon Jachim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors and strong typing
The fact that scala.actors are so dynamic and their messages untyped reminds me of this blogpost of Ola Bini: http://ola-bini.blogspot.com/2008/06/fractal-programming.html
Hmmm... It's a bit like a second level, dynamic language on top of the solid, static Scala kernel.
Maybe a next layer of abstraction could be a custom, internal DSL used to specify which actors should be created, how to route messages between them...
For me it sound like a much better solution than fashionable ESBs, SOAs...
Szymon

On Wed, Mar 18, 2009 at 11:37 PM, Alex Boisvert <boisvert@intalio.com> wrote:
On Wed, Mar 18, 2009 at 2:24 PM, Chris Lambrou <chris@lambrou.net> wrote:
I'm fairly new to Scala, and very much like what I've encountered so far. However, I'm puzzled by the apparent lack of type-safety when dealing with actors. Almost everything seems to be type-checked by the Scala compiler, except when it comes to actors. It appears that anything can be sent as a message to an actor, and the actor then fails at runtime if it's unable to handle the message. Is this indeed the case, or am I missing something? Is there a way to describe the type of messages that an actor will accept, and have this verified by the compiler? If not, are there any useful guidelines to follow to help avoid unwanted messages being passed to actors?

Hi Chris,

2 quick comments,

1) you can easily avoid failure in your actor by having a catch-all case clause, e.g.

react {
  case Foo => ...
  case Bar => ...
  case _ @ msg => Logger error "Unexpected message: %s".format(msg)
}

2) you can also easily provide a typed interface to your clients, e.g.,

trait SomeTypedActor {
  protected val actor: OutputChannel[Any]

  def foo(x: X) { actor ! Foo(x) }
  def bar(x: X): Y = { actor !? Bar(x) }
}

I imagine somebody will eventually come up with a more convenient and general solution for the above using a little bit of reflection and proxy magic.

alex
 



--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
Chris Lambrou
Joined: 2009-01-11,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors and strong typing
Ah, that was the approach I'd already begun to settle upon - that of encapsulating the actor and providing type-safe methods for posting messages. I haven't worked out how to constrain the type of replies to messages, though. Both the sender and reply methods of Actor are weakly typed, which makes things difficult. On the other hand, I'm also having second thoughts about the inflexibility that may arise when trying to constrain the types of actors and messages in a large graph. Compile-time type checking is good, but not if it begins to seriously hamper development.

Thanks for the responses. They've given me plenty to think about.

Chris


2009/3/18 Alex Boisvert <boisvert@intalio.com>

2) you can also easily provide a typed interface to your clients, e.g.,

trait SomeTypedActor {
  protected val actor: OutputChannel[Any]

  def foo(x: X) { actor ! Foo(x) }
  def bar(x: X): Y = { actor !? Bar(x) }
}

I imagine somebody will eventually come up with a more convenient and general solution for the above using a little bit of reflection and proxy magic.

alex
 

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 4 days ago.
Re: Actors and strong typing
Also look at scala.actors.Channel for a form of typed communication between Actors.

--j

On Wed, Mar 18, 2009 at 2:24 PM, Chris Lambrou <chris@lambrou.net> wrote:
I'm fairly new to Scala, and very much like what I've encountered so far. However, I'm puzzled by the apparent lack of type-safety when dealing with actors. Almost everything seems to be type-checked by the Scala compiler, except when it comes to actors. It appears that anything can be sent as a message to an actor, and the actor then fails at runtime if it's unable to handle the message. Is this indeed the case, or am I missing something? Is there a way to describe the type of messages that an actor will accept, and have this verified by the compiler? If not, are there any useful guidelines to follow to help avoid unwanted messages being passed to actors?

Chris

sadie
Joined: 2008-12-21,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors and strong typing

Chris Lambrou-4 wrote:
>
> I'm puzzled by the apparent lack of type-safety when dealing with actors.

I second the puzzlement. As the other replies have said, there are perfectly
good pattern for working around this lack of type safety, and I quickly
accustomed myself to adopting them - but the puzzlement is still there. It
seemed even more puzzling on replies to the !? message. A given specific
message would surely have replies of a known type, or a union of known types
(count or error message, say).

Is this to do with the coupling between actors residing separate VM
instances, or even separate computers entirely, and running different
versions of the code? If so, it would make a little more sense.

sadie
Joined: 2008-12-21,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors and strong typing

Chris Lambrou-4 wrote:
>
> I'm puzzled by the apparent lack of type-safety when dealing with actors.

I second the puzzlement. As the other replies have said, there are perfectly
good pattern for working around this lack of type safety, and I quickly
accustomed myself to adopting them - but the puzzlement is still there. It
seemed even more puzzling on replies to the !? message. A given specific
message would surely have replies of a known type, or a union of known types
(count or error message, say).

Is this to do with the coupling between actors residing separate VM
instances, or even separate computers entirely, and running different
versions of the code? If so, it would make a little more sense.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Actors and strong typing

On Thu, Mar 19, 2009 at 07:07:18PM -0700, Marcus Downing wrote:
> I second the puzzlement. As the other replies have said, there are
> perfectly good pattern for working around this lack of type safety,
> and I quickly accustomed myself to adopting them - but the puzzlement
> is still there.

One may not necessarily be convinced by the reasoning, but this has come
up before:

http://www.nabble.com/-scala--Why-are-Scala%27s-actors-untyped--tc188114...

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors and strong typing
FWIW, I chatted with Philip Wadler about this issue when he was in SF giving a presentation on Unityped vs. Statically typed languages.
He said that he tried to type the input and output channels and it became complex and was not useful.  I take him at his word.

On Thu, Mar 19, 2009 at 8:14 PM, Paul Phillips <paulp@improving.org> wrote:
On Thu, Mar 19, 2009 at 07:07:18PM -0700, Marcus Downing wrote:
> I second the puzzlement. As the other replies have said, there are
> perfectly good pattern for working around this lack of type safety,
> and I quickly accustomed myself to adopting them - but the puzzlement
> is still there.

One may not necessarily be convinced by the reasoning, but this has come
up before:

 http://www.nabble.com/-scala--Why-are-Scala%27s-actors-untyped--tc18811476r1.html

--
Paul Phillips      | Beware of bugs in the above code; I have only
Imperfectionist    | proved it correct, not tried it.
Empiricist         |     -- Knuth
up hill, pi pals!  |----------* http://www.improving.org/paulp/ *----------



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

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