Sending messages to functions in Scala
I'm working my way through Bruce Tate's Seven Languages in Seven Weeks and am having a hard time understanding his implementation of sizer.scala
(Scala: Day 3). In particular, consider the following Singleton object
object PageLoader {
def getPageSize(url : String) = Source.fromURL(url).mkString.length
}
and the following method that, using actors, calculates the number of characters in each web page given by the urls
array.
def getPageSizeConcurrently() = {
val caller = self
for(url <- urls) {
actor { caller ! (url, PageLoader.getPageSize(url)) }
}
for(i <- 1 to urls.size) {
receive {
case (url, 开发者_Go百科size) =>
println("Size for " + url + ": " + size)
}
}
}
- What does self refer to?
getPageSizeConcurrently
? Is it possible for self to refer to a function? - Assuming that self does refer to
getPageSizeConcurrently
, is this considered to be pretty standard in the Scala world? Why send messages to a function instead of an object, or vice versa?
UPDATE: The code in question only uses self once, but it does start with the following import
statements.
import scala.io._
import scala.actors._
import Actor._
Looking through the Scala API, it appears that the Actor
singleton object has a self
method. Even if that's the self
assigned to caller
, though, I don't see why the receive
block would be executed.
There is a self method on the Actor companion object. From the scaladoc:
Returns the currently executing actor. Should be used instead of this in all blocks of code executed by actors.
I'm guessing that your code has imported the Actor object, and that it is the self method on the Actor object your code is calling. This way you get a reference to the main actor thread you're in, and the anonymous actors you start to get page size can send the message back to the thread you're in.
self
is not a Scala keyword.
Although I don't have the book, Scala classes allow aliases for themselves; self
is commonly chosen. Here's why you might want to do that (not counting that you can restrict the type that the class can be when you specify the alias):
class A {
self =>
val a = 7
class B {
val a = 7 // Uh-oh, we've shadowed the parent class a.
val outerA = self.a // Whew, we haven't lost it!
}
}
So, self
is almost certainly the class that implements the getPageSizeConcurrently
method.
Without seeing more of the code, I don't have any insight into why exactly it's written this way (looks kind of strange to me). But there's no weird message-to-method going on here.
(Incidentally, note that it is formally possible to define an actor that extends a Function
trait. So you could, in fact, send a message to a function (function object, not a method). But the syntax wouldn't look like what you've got above.)
Although actors are not bound to threads as they were in the earlier versions of Scala, self
is comparable to Thread.current
. You use it, because Thread.current
does not have the necessary methods to view the current thread as an actor.
"What class or object has getPageSizeConcurrently as one of its methods?" - Rex Kerr
"Nothing, as far as I can tell..." - Chris
I assume self
tries to treat the current thread as an actor.
Note: Be careful, when using self
in the REPL.
精彩评论