Azula
Azula

Reputation: 467

Can I create empty Tuple or String in Scala?

I need return tuple , but if something goes wrong I want return empty tuple, like Seq.empty[Type] or Nil in Lists. Is it possible? Also I am curious about is it possible with String type.

Upvotes: 4

Views: 20925

Answers (3)

Sasha
Sasha

Reputation: 4004

Can I create empty Tuple in Scala?

In Scala 3, empty tuple is represented by case object EmptyTuple. Also empty tuple is returned by Tuple() (i.e. Tuple.apply(), without parameters).

val v = Tuple()
println(v == EmptyTuple) // true
println(v.isInstanceOf[Tuple]) // true
println(v.productArity) // 0

Also (offtopic) you can create a tuple of size 1 by any of the following ways:

  • Tuple(myValue), Tuple.apply(myValue), Tuple.apply[MyValueType](myValue),
  • Tuple1(myValue), Tuple1[MyValueType](myValue),
  • myValue *: EmptyTuple, myValue *: Tuple().

The type of result is Tuple1[MyValueType] a.k.a. MyValueType *: EmptyTuple (it's a subtype of NonEmptyTuple, which is, of course, a subtype of Tuple).

Can I create empty String in Scala?

Sure.

Upvotes: 1

Since you need to return an a value that can go wrong. In Scala the recommended way to deal with this is returning an Option, Try or Either value.

For instance:

def somethingThatCanGoWrongWithTry(): Try[(Int, String)] = {
  Try{
    val intValue = sideEffectValueInt()
    val stringValue = sideEffectValueString()
    (intValue, stringValue)
  }
}

def somethingThatCanGoWrongWithOption(): Option[(Int,String)] = {
  Try {
    val intValue = sideEffectValueInt()
    val stringValue = sideEffectValueString()
    (intValue, stringValue)
  }.toOption
}

def somethingThatCanGoWrongWithEither(): Either[Oops, (Int,String)] = {
  Try {
    val intValue = sideEffectValueInt()
    val stringValue = sideEffectValueString()
    (intValue, stringValue)
  } match {
    case Success(value) => Right(value)
    case Failure(exception) => Left(Oops(exception))
  }
}

Upvotes: 1

Jörg W Mittag
Jörg W Mittag

Reputation: 369458

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 = ""

Upvotes: 13

Related Questions