Why is there no Tuple1 Literal for single element tuples in Scala?

后端 未结 3 1140
野趣味
野趣味 2021-01-17 12:31

Python has (1,) for a single element tuple. In Scala, (1,2) works for Tuple2(1,2) but we must use Tuple1(1) to get a sin

3条回答
  •  一整个雨季
    2021-01-17 12:37

    You could, of course, add an implicit conversion to your API:

    implicit def value2tuple[A](x: A) = Tuple1(x)
    

    I do find it odd that Tuple1.toString includes the trailing comma:

    scala> Tuple1(1)
    res0: (Int,) = (1,)
    

提交回复
热议问题