Effective Kotlin: Item 30 — Favor generic methods

Matthew Dolan
1 min readDec 16, 2018

In the incredible Effective Java, item 30 continues from the previous item covering generic methods in preference to a client needing explicit casts on input parameters and return values.

The implementation in Kotlin is not much different to Java just with the type parameter list specified between fun and the function name.

// Generic method
fun <T> union(vararg sets: Set<T>): Set<T> {
return sets.flatMap { it }.toSet()
}
val union = union<Number>(setOf<Int>(1, 2), setOf<Double>(3.1, 4.2))

--

--

Matthew Dolan

Matt Dolan has been eating doughnuts and developing with Android since the dark days of v1.6.