Why is Scala's syntax for tuples so unusual?

前端 未结 7 1201
广开言路
广开言路 2020-12-08 13:10

In mathematics and computer science, a tuple is an ordered list of elements. In set theory, an (ordered) n-tuple is a sequence (or ordered list) of n elem

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 13:49

    One big difference between List, Seq or any collection and tuple is that in tuple each element has it's own type where in List all elements have the same type.

    And as consequence, in Scala you will find classes like Tuple2[T1, T2] or Tuple3[T1, T2, T3], so for each element you also have type parameter. Collections accept only 1 type parameter: List[T]. Syntax like ("Test", 123, new Date) is just syntactic sugar for Tuple3[String, Int, Date]. And _1, _2, etc. are just fields on tuple that return correspondent element.

提交回复
热议问题