- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Actor.self in 2.9.0 REPL
Wed, 2011-05-25, 16:44
Hi All,
I have run into a difference between using Actor.self in the 2.9.0 REPL compared to the 2.8.1 REPL. I was unable to find an issue in trac, though it may be because I don't know how to search them correctly. It may also be intentional. Here is the difference:
Scala 2.8.1 REPL:
scala> import scala.actors.Actor._
import scala.actors.Actor._
scala> self ! "hello"
scala> self.receiveWithin(0) { case x => x }
res12: Any = hello
But Scala 2.9.0 REPL:
scala> import scala.actors.Actor._
import scala.actors.Actor._
scala> self ! "hello"
scala> self.receiveWithin(0) { case x => x }
res9: Any = TIMEOUT
No matter how many times I send a message to self in the REPL, I can't seem to receive it. Is this a bug? If so I'll submit it to Trac.
Thanks.
Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com
I have run into a difference between using Actor.self in the 2.9.0 REPL compared to the 2.8.1 REPL. I was unable to find an issue in trac, though it may be because I don't know how to search them correctly. It may also be intentional. Here is the difference:
Scala 2.8.1 REPL:
scala> import scala.actors.Actor._
import scala.actors.Actor._
scala> self ! "hello"
scala> self.receiveWithin(0) { case x => x }
res12: Any = hello
But Scala 2.9.0 REPL:
scala> import scala.actors.Actor._
import scala.actors.Actor._
scala> self ! "hello"
scala> self.receiveWithin(0) { case x => x }
res9: Any = TIMEOUT
No matter how many times I send a message to self in the REPL, I can't seem to receive it. Is this a bug? If so I'll submit it to Trac.
Thanks.
Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com
Thu, 2011-05-26, 17:27
#2
Re: Actor.self in 2.9.0 REPL
Hi Maarten,
On Thu, May 26, 2011 at 1:54 AM, Maarten Hazewinkel <maarten.hazewinkel@gmail.com> wrote:
Thanks for the explanation. I think that sounds more like a feature not a bug, but I'd like to see if there's a workaround. I tried capturing one particular self and using it and that got me an exception:
scala> import scala.actors.Actor._
import scala.actors.Actor._
scala> val myself = self
myself: scala.actors.Actor = scala.actors.ActorProxy@28892a87
scala> myself ! "hello"
scala> myself.receiveWithin(0) { case x => x }
java.lang.AssertionError: assertion failed: receive from channel belonging to other actor
at scala.Predef$.assert(Predef.scala:103)
at scala.actors.Actor$class.receiveWithin(Actor.scala:555)
at scala.actors.ActorProxy.receiveWithin(ActorProxy.scala:20)
at .<init>(<console>:12)
at .<clinit>(<console>)
Does anyone know a way to use Actor.self in the 2.9.0 REPL?
Bill
--
Bill Venners
Artima, Inc.
http://www.artima.com
On Thu, May 26, 2011 at 1:54 AM, Maarten Hazewinkel <maarten.hazewinkel@gmail.com> wrote:
Hi Bill,
The reason for this is that self keeps changing for each line entered in the REPL.
You can execute 'println(self)' a few times to see that happening.
The deeper reason behind that is that 'self' is kept as a ThreadLocal, and in the 2.9 REPL, each statement is executed in a new thread.
You can see that by running a few 'println(Thread.currentThread)' statements.
It's probably worth writing up a bug report for this, since I doubt that you're going to be the last person to be surprised by this.
Thanks for the explanation. I think that sounds more like a feature not a bug, but I'd like to see if there's a workaround. I tried capturing one particular self and using it and that got me an exception:
scala> import scala.actors.Actor._
import scala.actors.Actor._
scala> val myself = self
myself: scala.actors.Actor = scala.actors.ActorProxy@28892a87
scala> myself ! "hello"
scala> myself.receiveWithin(0) { case x => x }
java.lang.AssertionError: assertion failed: receive from channel belonging to other actor
at scala.Predef$.assert(Predef.scala:103)
at scala.actors.Actor$class.receiveWithin(Actor.scala:555)
at scala.actors.ActorProxy.receiveWithin(ActorProxy.scala:20)
at .<init>(<console>:12)
at .<clinit>(<console>)
Does anyone know a way to use Actor.self in the 2.9.0 REPL?
Bill
Kind regards,
Maarten
On 25 May, 2011,at 05:44 PM, Bill Venners <bill@artima.com> wrote:Hi All,
I have run into a difference between using Actor.self in the 2.9.0 REPL compared to the 2.8.1 REPL. I was unable to find an issue in trac, though it may be because I don't know how to search them correctly. It may also be intentional. Here is the difference:
Scala 2.8.1 REPL:
scala> import scala.actors.Actor._
import scala.actors.Actor._
scala> self ! "hello"
scala> self.receiveWithin(0) { case x => x }
res12: Any = hello
But Scala 2.9.0 REPL:
scala> import scala.actors.Actor._
import scala.actors.Actor._
scala> self ! "hello"
scala> self.receiveWithin(0) { case x => x }
res9: Any = TIMEOUT
No matter how many times I send a message to self in the REPL, I can't seem to receive it. Is this a bug? If so I'll submit it to Trac.
Thanks.
Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com
--
Bill Venners
Artima, Inc.
http://www.artima.com
Thu, 2011-05-26, 18:17
#3
Re: Actor.self in 2.9.0 REPL
You can use it in an object:
scala> object Test {
| self ! "hello"
| self.receiveWithin(0) { case x => println(x) }
| }
defined module Test
scala> Test
hello
res5: Test.type = Test$@308a58a5
Otherwise, a different thread is created for each line and it doesn't work well with "self" that uses thread local storage.
Cheers
Sylvain
Le 26/05/2011 18:12, Bill Venners a écrit :
scala> object Test {
| self ! "hello"
| self.receiveWithin(0) { case x => println(x) }
| }
defined module Test
scala> Test
hello
res5: Test.type = Test$@308a58a5
Otherwise, a different thread is created for each line and it doesn't work well with "self" that uses thread local storage.
Cheers
Sylvain
Le 26/05/2011 18:12, Bill Venners a écrit :
BANLkTin+-zG1UN-Vt6QuGawqDV3E7qwMWw [at] mail [dot] gmail [dot] com" type="cite">
Does anyone know a way to use Actor.self in the 2.9.0 REPL?
Bill
-- Sylvain HENRY PhD Student INRIA/LaBRI RunTime Team Tel: +33 (0)6-70-94-86-76 http://hsyl20.fr
Thu, 2011-05-26, 18:27
#4
Re: Actor.self in 2.9.0 REPL
Even better:
scala> import scala.actors.Actor._
import scala.actors.Actor._
scala> {
| self ! "hello"
| self.receiveWithin(0) { case x => println(x) }
| }
hello
Cheers
Sylvain
Le 26/05/2011 18:57, Sylvain HENRY a écrit :
scala> import scala.actors.Actor._
import scala.actors.Actor._
scala> {
| self ! "hello"
| self.receiveWithin(0) { case x => println(x) }
| }
hello
Cheers
Sylvain
Le 26/05/2011 18:57, Sylvain HENRY a écrit :
4DDE8681 [dot] 9090903 [at] gmail [dot] com" type="cite"> You can use it in an object:
scala> object Test {
| self ! "hello"
| self.receiveWithin(0) { case x => println(x) }
| }
defined module Test
scala> Test
hello
res5: Test.type = Test$@308a58a5
Otherwise, a different thread is created for each line and it doesn't work well with "self" that uses thread local storage.
Cheers
Sylvain
Le 26/05/2011 18:12, Bill Venners a écrit :BANLkTin+-zG1UN-Vt6QuGawqDV3E7qwMWw [at] mail [dot] gmail [dot] com" type="cite">
Does anyone know a way to use Actor.self in the 2.9.0 REPL?
Bill
-- Sylvain HENRY PhD Student INRIA/LaBRI RunTime Team Tel: +33 (0)6-70-94-86-76 http://hsyl20.fr
The reason for this is that self keeps changing for each line entered in the REPL.
You can execute 'println(self)' a few times to see that happening.
The deeper reason behind that is that 'self' is kept as a ThreadLocal, and in the 2.9 REPL, each statement is executed in a new thread.
You can see that by running a few 'println(Thread.currentThread)' statements.
It's probably worth writing up a bug report for this, since I doubt that you're going to be the last person to be surprised by this.
Kind regards,
Maarten
On 25 May, 2011,at 05:44 PM, Bill Venners <bill@artima.com> wrote: