- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Actor as instance variable of another actor
Tue, 2010-09-14, 09:19
Dear Scala users,
I have a question about actors. I would like to have an actor, that itself has an anonymous actor as an instance variable, which only listens to feedback from other actors.
The problem I’m having is that the child actor seems to share the mailbox of its parent, and I get an “assertion failed: receive from channel belonging to other actor”. Is there no means to do what I’m trying to do? (Of course I could handle the message in the parent actor, but the plan was not to overload the parent mailbox with low-level messages.) But perhaps I miss something basic about actor design.
As a side question, I’m wondering whether reply/!? must go together, or we can use reply even if not waiting for the response on the other side?
Many thanks for your help,
Gilles.
Tue, 2010-09-14, 18:37
#2
Re: Actor as instance variable of another actor
Gilles,
How about something like:
scala> class MyMainActor extends Actor {
|
| object MyHandlerActor extends Actor {
| start()
| def act() {
| loop {
| react {
| case e => println("Got: " + e)
| }
| }
| }
| }
|
| def act() {
| loop {
| react {
| case e => MyHandlerActor forward e
| }
| }
| }
| }
defined class MyMainActor
scala> val m = (new MyMainActor).start()
m: scala.actors.Actor = MyMainActor@1f75442
scala> m ! "Hello"
Got: Hello
On Tue, Sep 14, 2010 at 1:11 AM, Gilles SCOUVART <Gilles.SCOUVART@n-side.be> wrote:
How about something like:
scala> class MyMainActor extends Actor {
|
| object MyHandlerActor extends Actor {
| start()
| def act() {
| loop {
| react {
| case e => println("Got: " + e)
| }
| }
| }
| }
|
| def act() {
| loop {
| react {
| case e => MyHandlerActor forward e
| }
| }
| }
| }
defined class MyMainActor
scala> val m = (new MyMainActor).start()
m: scala.actors.Actor = MyMainActor@1f75442
scala> m ! "Hello"
Got: Hello
On Tue, Sep 14, 2010 at 1:11 AM, Gilles SCOUVART <Gilles.SCOUVART@n-side.be> wrote:
Dear Scala users,
I have a question about actors. I would like to have an actor, that itself has an anonymous actor as an instance variable, which only listens to feedback from other actors.
The problem I’m having is that the child actor seems to share the mailbox of its parent, and I get an “assertion failed: receive from channel belonging to other actor”. Is there no means to do what I’m trying to do? (Of course I could handle the message in the parent actor, but the plan was not to overload the parent mailbox with low-level messages.) But perhaps I miss something basic about actor design.
As a side question, I’m wondering whether reply/!? must go together, or we can use reply even if not waiting for the response on the other side?
Many thanks for your help,
Gilles.
I think code would help everyone here.
On Tue, Sep 14, 2010 at 10:11 AM, Gilles SCOUVART <Gilles.SCOUVART@n-side.be> wrote:
--
Viktor Klang,
Code Connoisseur
Work: www.akkasource.com
Code: github.com/viktorklang
Follow: twitter.com/viktorklang
Read: klangism.tumblr.com