Sending messages to functions in Scala

拥有回忆 提交于 2020-01-03 13:40:08

问题


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, size) =>
                println("Size for " + url + ": " + size)
        }
    }
}
  1. What does self refer to? getPageSizeConcurrently? Is it possible for self to refer to a function?
  2. 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.


回答1:


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.




回答2:


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.)




回答3:


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.



来源:https://stackoverflow.com/questions/6743169/sending-messages-to-functions-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!