You should use tuples when it is the most appropriate level of abstraction.
For example, if you are writing a framework where everything is completely generic, and the most concrete things that you can get are "functions", "cartesian products", "semigroupals", or something like "closed monoidal categories", then you probably will have no other choice but using Tuple
. Here is one good example: Semigroupal with method:
product[A, B](fa: F[A], fb: F[B]): F[(A, B)]
Here, the usage of the tuple type is completely justified, because the interface is sufficiently abstract.
Another example: in the entire Scala collection library, you will never encounter anything as concrete as Person(name: String, surname: String)
, but instead you will see a huge lot of methods that work on tuples of generic types (A, B)
.
However, if the most abstract thing in your code is
object Fred {
val name = "Fred"
val surname = "Bloggs"
}
and then you suddenly find out that you also need another similar object for John Doe
, it would be much more appropriate to define
case class Person(name: String, surname: String)
and then maybe even to use it like this:
val j = Person(name = "John", surname = "Doe")
Of course, there is great value in accurately modeling the domain. These two goals -- accurately modeling a concrete domain vs. writing generic frameworks that work in as many cases as possible -- are complementary.
To summarize:
- Are you writing a sufficiently abstract completely generic framework? Use tuples.
- Are you trying to model some concrete domain as closely as possible? Use appropriately named case classes.
This here:
type Person = (String, String) //firstName, lastName
looks like a mismatch between the domain and the used abstraction. You can see it simply from the fact that the compiler does not prevent you from accidentally swapping the two components. In contrast, if you worked at a different abstraction level with generic A
and B
types, the compiler would emit an error message if you accidentally swapped (B, A)
where an (A, B)
was expected.