[2023] Kotlin Tutorial
Article directory
Part II Object Oriented and Functional Programming
Chapter 14 Generics
Use generics to maximize code reuse, protect type safety, and improve performance. The biggest impact of the generic feature on Kotlin is the use of generics in collections.
14.1 Generic functions
Generics can be applied to function declarations, property declarations, generic classes, and generic interfaces.
14.1.2 Multiple Type Parameters
Our previous generic function example used only one type parameter,
In fact, multiple type parameters can be declared at the same time, separated by commas ",", examples are as follows:
fun <T,U> addRectangle(a:T,b:U):Boolean {...}
Type parameters can not only declare the function parameter type, but also declare the return type of the function. The sample code is as follows:
fun <T,U> rectangleEquals(a:T,b:U):U {...}