Scala student Scala student - 2 months ago 9x
Scala Question

Can I create empty Tuple or String in Scala?

I need return tuple , but if something goes wrong I want return empty tuple, like

in Lists. Is it possible?
Also I am curious about is it possible with String type.


I want return empty tuple

That doesn't make sense. A tuple is not a collection. The idea of "emptiness" is simply non-sensical. The length of a tuple is part of its type. So, an empty tuple is a different type than a non-empty tuple.

In fact, it doesn't even make sense to talk about "tuple" as a type. There are pairs (2-tuples aka Tuple2), triples (3-tuples aka Tuple3), quadruples, quintuples, sixtuples, septuples, octuples, 9-tuples, 10-tuples, etc, and they are all different types.

Also, more or less the only sensible thing you can do with a tuple is to extract its elements. So, for a tuple with no elements, there is pretty much no sensible thing you can do. What use is a structure that holds no values and has no operations? It's completely useless.

In fact, a 0-tuple is isomorphic to the Unit value, the value which denotes the absence of a useful value. In Scala, and also Haskell, the syntax for an empty tuple is actually used for denoting the Unit value:

val u = ()
// => u: Unit = ()

Also I am curious about is it possible with String type.

Yes, of course, you can have an empty string:

val s = ""
// => s: String = ""