Perhaps, no new syntax is necessary whatsoever.
Currently there are tuple constructors in the object Tuple
already, e.g.:
scala> Tuple()
val res0: EmptyTuple.type = ()
scala> Tuple("one")
val res1: String *: EmptyTuple = (one,)
A sad thing though is that it only works for arities 0 and 1:
scala> Tuple("one", "two")
-- [E134] Type Error: ----------------------------------------------------------
1 |Tuple("one", "two")
|^^^^^
|None of the overloaded alternatives of method apply in object Tuple with types
| [T](x: T): T *: EmptyTuple
| (): EmptyTuple
|match arguments (("one" : String), ("two" : String))
|
|where: T is a type variable
1 error found
Perhaps, if Scala could get Tuple#apply
methods for other arities up to 22 then Tuple( <arg-list> )
could become a mainstream general purpose way to construct tuples in place.
Moreover, it would line-up with the List
syntax in a good way (if we think about tuples as heterogenous lists), i.e.:
1 :: 2 :: 3 :: Nil <=> List(1, 2, 3)
likewise
1 *: "2" *: 3.45 *: EmptyTuple <=> Tuple(1, "2", 3.45)
(if there was Tuple#apply
for 3 args, apparently).
In other words, extending the standard library by adding more apply
to object Tuple
may look simpler and less controversial (and more intuitive) way rather than modifying Scala syntax itself (along with all the corresponding downstream stuff) just to support 1 particular case for arity 1.
Unless the goal is to provide the extreme level of conciseness, of course, saving the whole 4 characters of typing:
Tuple(1)
(1,) // 4 chars shorter
But in the latter case there’s still a question on how to support empty tuples, because Tuple()
“just works” but ()
is not a tuple either. Would Scala consider adding support for (,)
syntax too?