What is is “(x, y)” in Scala, and what is actually being returned?

前端 未结 2 1372
无人及你
无人及你 2021-01-22 16:56
scala> def check(a: Int, b: Int): (Int, Int) = {
      (3, 4)
      }

The returning type is (Int, Int). How is this possible? What is S

相关标签:
2条回答
  • 2021-01-22 17:16

    As @dhg mentioned, (Int, Int) is equals to Tuple2[Int, Int]

    In second sample you use pattern matching in variable definition. You can use it with tuples, case classes and with everything which has extractors. Actually, everything works via extractors.

    scala> val p = Point(1, 2)
    p: Point = Point(1,2)
    
    scala> val Point(x, y) = p
    x: Int = 1
    y: Int = 2
    
    scala> val Property = "(.+)=(.+)".r
    Property: scala.util.matching.Regex = (.+)=(.+)
    
    scala> val Property(name, value) = "name1=value1"
    name: String = name1
    value: String = value1
    
    0 讨论(0)
  • 2021-01-22 17:29

    The type (Int, Int) is just a nicer way of writing Tuple2[Int,Int]

    0 讨论(0)
提交回复
热议问题