🐦 Kotlin Advanced

What is a star projection in Kotlin generics?

Answer

A star projection (*) is used when you want to refer to a generic type without specifying the type argument — similar to Java's ? wildcard. List<*> means "a list of some type I don't know." You can read elements as Any? but cannot write to the list. Each type parameter with different variance produces different star projection behavior: for an out T parameter, * acts like out Any?; for an in T parameter, * acts like in Nothing. Use star projections when the actual type argument is irrelevant to the operation you're performing.