I'd like more clarity in understanding the place Tuple2 class in Scala 3.3.1.
In its source code, what is clear is that this class inherits the Product2 trait. What remains doubt to me is how it comes into being that Tuple2 also inherits immediately from:
- class T1 *: T2 *: EmptyTuple.type
- trait Serializable
I haven't been able understood that.
I've read through relevant Scala doc and source code. I can understand the overall design idea of inheritance of these hierarchy but I haven't understood the more nuanced points (the question as an example)
Let's not confuse classes and types, inheritance (subclassing) and subtyping
What is the difference between Type and Class?
What is the difference between a class and a type in Scala (and Java)?
https://typelevel.org/blog/2017/02/13/more-types-than-classes.html
What is the difference between subtyping and inheritance in OO programming?
https://counterexamples.org/subtyping-vs-inheritance.html
From OOP point of view,
Tuple2extends (is a subclass of)Product2,Product2extendsProduct. And*:extendsNonEmptyTuple,NonEmptyTupleandEmptyTupleextendTuple. Two independent OOP hierarchies.Making
A *: B *: EmptyTuplea subtype ofTuple2[A, B]and vice versa (and providing syntax sugar(A, B)) is a compiler magic. According to the spec,https://scala-lang.org/files/archive/spec/3.4/03-types.html#tuple-types
https://scala-lang.org/files/archive/spec/3.4/03-types.html#conformance
In the compiler, subtyping is implemented in
TypeComparerhttps://dotty.epfl.ch/docs/internals/type-system.html#subtyping-checks-1
https://github.com/lampepfl/dotty/blob/main/compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Regarding
Serializable, all case classes extendProductandSerializableandTuple2is a case class. You can switch onscalacOptions ++= Seq("-Vprint:typer", "-Xprint-types") and seeHere is explanation of generating sugar for case classes in Scala 2 but in Scala 3 this should be similar
How can I view the code that Scala uses to automatically generate the apply function for case classes?
(so one can find a specific place where
extends Serializableis added).