0

次のコードの何が問題になっていますか? 関数への入力のタイプとしてタプル (String, Int) を使用しようとしていますfind_host。コンパイラではエラーは発生しませんが、プログラムを実行するとエラーが発生します。ここで何が欠けていますか?

  def find_host ( f : (String, Int) ) =     {
    case ("localhost", 80 ) => println( "Got localhost")
    case _  => println ("something else")
  }

  val hostport = ("localhost", 80)

  find_host(hostport)

      missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: ?
  def find_host ( f : (String, Int) ) =     {
                                           ^
4

3 に答える 3

1

このコードはコンパイルに失敗します。IntelliJ の Scala サポートは完全ではありません。すべてのコンパイル エラーを見つけることは期待できません。

これを REPL で試すと、次のようになります。

scala>   def find_host ( f : (String, Int) ) =     {
     |     case ("localhost", 80 ) => println( "Got localhost")
     |     case _  => println ("something else")
     |   }
<console>:7: error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: ?
         def find_host ( f : (String, Int) ) =     {
                                                   ^

Shadowlands の答えが言うようf matchに、部分関数の前に行方不明です。

ただし、このメソッドは を返すUnitため、等号で定義しないでください。

def find_host(f: (String, Int)) {
  f match {
    case ("localhost", 80) => println("Got localhost")
    case _  => println("something else")
  }
}
于 2013-09-03T04:35:13.270 に答える