Syntax for type tuple with one element

Indeed, and I think this is also something that beginners could find difficult because of its non-orthogonality. In fact, any time you have learned a syntax rule of a new language, the exceptions are the ones causing the headaches. Given you know, for example, that a sequence of numbers between brackets constitute a Tuple, and most instantiations can be done via the class name, these are highly irregular:

val t0 = Tuple()  // () is unit
val t1 = Tuple(1) // (1) is not a Tuple
val t2 = (1,2)    // Tuple(1,2) not allowed

Welcome to Scala!

If we start accepting a trailing comma (which looks horrible to me) we might enter the same issues:

val t0 = (,)    // newcomer: what is this??
val t1 = (1,)   // fair, it's not just a number in brackets ...
val t2 = (1,2)  // (1,2,) not allowed??

A possibility to make this simpler and more orthogonal would be to define the Tuple by double brackets:

val t0 = (())    // Empty Tuple, or use Tuple() which should work as well.
val t1 = ((1))   // Tuple with one element, or use Tuple(1) which should work as well. 
val t2 = ((1,2)) // Tuple with two elements, or use Tuple(1,2) which should work as well'

etc, and while we are at it use

val u: Unit = unit // drop the (), its immediately clear the unit is an instance of Unit (and only 2 chars more)

I am not saying we should change this for that would cause other issues, but rather that we should keep orthogonality more in (on top of) mind when we are making changes to the language.

3 Likes