2

书中的代码:

object NameResolver extends Actor{
    import java.net.{InetAddress, UnknownHostException}

    def act(){
        react{
            case(name:String, actor:Actor) =>
                actor ! getIp(name)
                act()
            case "EXIT" => 
                println("Name resolver is exiting.")//quit
            case msg =>
                println("Unhandled message: "+msg)
                act()
        }
    }

    def getIp(name:String):Option[InetAddress]={
        try{
            Some(InetAddress.getByName(name))
        } catch {
            case _ : UnknownHostException => None
        }
    }

}

口译行:

NameResolver ! ("http://stackoverflow.com", self)

但我得到了错误:

scala> NameResolver ! ("http://stackoverflow.com", self)
<console>:8: error: too many arguments for method !: (msg: Any)Unit
              NameResolver ! ("http://stackoverflow.com", self)

根据书,它应该工作。我从书中复制的代码中是否存在会导致异常的错误?

4

1 回答 1

3

您正在尝试传递一个元组,但编译器认为您正在!使用两个参数进行调用。因为有时可以省略括号,所以有时元组需要额外的括号,例如 in ((1, 2))。这可能会引起混淆。尝试NameResolver !(("http://stackoverflow.com", self))

于 2013-02-21T19:13:53.233 回答