Why is Scala's syntax for tuples so unusual?

前端 未结 7 1163
广开言路
广开言路 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:52

    You can easily achive that with shapeless:

    import shapeless.syntax.std.tuple._
    
    val t = ("a", 2, true, 0.0)
    
    val s = t(0) // String at compile time
    val i = t(1) // Int at compile time
    // etc
    

    A lot of methods available for standard collection are also available for tuples this way (head, tail, init, last, ++ and ::: for concatenation, +: and :+ for adding elements, take, drop, reverse, zip, unzip, length, toList, toArray, to[Collection], ...)

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