1

行動を理解するのを手伝ってもらえますか

  //Creating a tuple
  val myTuple = ("Sudipta","Deb","Switzerland",1234)
                                                  //> myTuple  : (String, String, String, Int) = (Sudipta,Deb,Switzerland,1234)
  myTuple._2                                      //> res0: String = Deb
  myTuple._4                                      //> res1: Int = 1234
  val (first, second, third, fourth) = myTuple    //> first  : String = Sudipta
                                                  //| second  : String = Deb
                                                  //| third  : String = Switzerland
                                                  //| fourth  : Int = 1234
  //val (first1, second1, _) = myTuple

最後の行でエラーが発生しました:

constructor cannot be instantiated to expected type;  found   : (T1, T2, T3)  required: (String, String, String, Int)

私の質問は、なぜこのように動作するのですか? Scala for Impatient Book には、次のように書かれています。

You can use a _ if you don’t need all components:
val (first, second, _) = t

参考までに、完全なコードを見たい場合は、私の GitHub リポジトリにあります。リンク: Scala ワークシート

4

1 に答える 1