What are higher-kinded types in Scala and why are they important?
Answer
Higher-kinded types (HKT) are type constructors that take other type constructors as parameters — types of types. A regular generic like List[A] has kind * -> * (takes one concrete type, returns a concrete type). An HKT parameter is F[_] — F is a type constructor of kind * -> *. Example: trait Functor[F[_]] { def map[A, B](fa: F[A])(f: A => B): F[B] }. This Functor type class works for any F that is a type constructor — List, Option, Future, Either[E, ?], etc. Implement: implicit val listFunctor: Functor[List] = new Functor[List] { def map[A, B](fa: List[A])(f: A => B) = fa.map(f) }. HKTs enable writing code that is polymorphic over the container type — the same function works for List[Int], Option[Int], Future[Int]. This is the foundation of the Cats and Scalaz functional programming libraries. HKTs are one of Scala's key advantages over Java for functional programming.
Previous
What is the Scala concurrency model compared to Java?
Next
What is opaque type alias in Scala 3?