What are Generics?
Generics allow you to write classes and methods that can work with any data type, while still maintaining full type safety. You use square brackets
[T] (unlike the <T> used in Java) to define a type parameter.scala
Variance: + and -
Wait! This is a unique and advanced feature of Scala.
+T(Covariant): IfDogis aAnimal, thenBox[Dog]is aBox[Animal].-T(Contravariant): IfDogis aAnimal, thenBox[Animal]is aBox[Dog].
scala
Type Bounds
Sometimes you want to restrict which types can be used.
T <: Animal(Upper Bound): T must be a subclass of Animal.T >: Dog(Lower Bound): T must be a superclass of Dog.
scala
Why use Generics?
- Type Safety: The compiler ensures you don't accidentally add the wrong type to a collection.
- Code Reuse: Write one list, one cache, or one repository for any type.
- Documentation: The types themselves serve as documentation for your code.
Summary
| Term | Description |
|---|---|
| [T] | The syntax for a type parameter |
| [+T] | Covariant: Preserves inheritance relationship |
| [-T] | Contravariant: Reverses inheritance relationship |
| <: | Upper bound (Subtype of...) |
| >: | Lower bound (Supertype of...) |
| Best For | Collections, Repositories, and complex Frameworks |
| Logic | "Abstract over types safely" |
| Safety | Catch type-related bugs at compile-time |
| Modern | Essential for building generic Big Data pipelines |
| Standard | Use T, U, A, B as conventional type names |
| Identity | Generics in Scala are checked at compile-time and erased at runtime |