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

Actors and typing

3 replies
Marc Weber
Joined: 2010-02-16,
User offline. Last seen 2 years 3 weeks ago.

Hi, I'm still new to both: Actors and Scala.

Is my understand correct that an Actor is kind of object I can throw a
message at which may or may not send back a reply?

Well that's fine. But why can't I restrict the type of messages which I
can throw at an actor or which an Actor will return?

Eg it would be no problem to define an Actor like this, would it?

class myActor extends Actor[ExpectedInput, OutputType] {
..
}

You can still use [Any,Any] if you want to allow everything, can't you?

Is there something I've missed?

eg when changing the return type from String to Either[Error, String] I
want my code to break at compile time.

Do other Actor implementations exist which provide this kind of static
checking?

Marc Weber

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors and typing


On Tue, Jul 6, 2010 at 10:32 AM, Marc Weber <marco-oweber [at] gmx [dot] de> wrote:
Hi, I'm still new to both: Actors and Scala.

Is my understand correct that an Actor is kind of object I can throw a
message at which may or may not send back a reply?

Well that's fine. But why can't I restrict the type of messages which I
can throw at an actor or which an Actor will return?

Eg it would be no problem to define an Actor like this, would it?

class myActor extends Actor[ExpectedInput, OutputType] {
 ..
}

You can still use [Any,Any] if you want to allow everything, can't you?

Is there something I've missed?

eg when changing the return type from String to Either[Error, String] I
want my code to break at compile time.

Do other Actor implementations exist which provide this kind of static
checking?

Lift has Actors that can be typed for message receipts.

Stambecco (built on Lift Actors) supports typed messages and typed messages with typed responses.
 

Marc Weber



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Actors and typing

trait AbstractActor extends OutputChannel[Any] with CanReply[Any, Any]

... you might be able to back your own typed actors from OutputChannel / CanReply / InputChannel and such ... not sure how feasible this is, though ...

also there is in Actor

def !![A](msg: Any, handler: PartialFunction[Any, A]): Future[A]

so you can create a helper method using the resulting future's inputChannel (which is typed) to create like a typed !? method....

best, -sciss-

Am 06.07.2010 um 18:32 schrieb Marc Weber:

> Hi, I'm still new to both: Actors and Scala.
>
> Is my understand correct that an Actor is kind of object I can throw a
> message at which may or may not send back a reply?
>
> Well that's fine. But why can't I restrict the type of messages which I
> can throw at an actor or which an Actor will return?
>
> Eg it would be no problem to define an Actor like this, would it?
>
> class myActor extends Actor[ExpectedInput, OutputType] {
> ..
> }
>
> You can still use [Any,Any] if you want to allow everything, can't you?
>
> Is there something I've missed?
>
> eg when changing the return type from String to Either[Error, String] I
> want my code to break at compile time.
>
> Do other Actor implementations exist which provide this kind of static
> checking?
>
> Marc Weber

Philipp Haller
Joined: 2009-01-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Actors and typing

Hi Marc,

On 07/06/2010 07:32 PM, Marc Weber wrote:
> Is my understand correct that an Actor is kind of object I can throw a
> message at which may or may not send back a reply?
>
> Well that's fine. But why can't I restrict the type of messages which I
> can throw at an actor or which an Actor will return?

The following thread discusses this:
http://article.gmane.org/gmane.comp.lang.scala/12988

The bottom line is that both the dynamically-scoped `self` as well as
`receiveWithin`/`reactWithin` cause problems with typed actors.

However, note that the `Reactor` trait (new in Scala 2.8) is
parameterized in the type of messages is accepts. Consequently,
`Reactor` neither supports `self` nor `reactWithin`.

> Do other Actor implementations exist which provide this kind of static
> checking?

You may be interested in a blog post by Josh Suereth that explains how
to use channels and partial functions to build input- and output-typed
instances of `scala.actors.Actor`:
http://suereth.blogspot.com/2010/02/actor-styles-oo-functional-or-blende...

However, in this case the behavior of an actor is defined by a partial
function that should not use `self` or `receiveWithin`.

Cheers,
Philipp

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