Introduction
Using multiple type parameters lets you create flexible functions or classes that work with different types at the same time.
fun <T, U> functionName(param1: T, param2: U): String { // function body }
fun <T, U> pairToString(first: T, second: U): String { return "First: $first, Second: $second" }
class Box<T, U>(val first: T, val second: U) { fun describe() = "Box contains: $first and $second" }
fun <T, U> combineToString(a: T, b: U): String { return "Combined: $a and $b" } fun main() { val result1 = combineToString(10, "apples") val result2 = combineToString("Hello", 3.14) println(result1) println(result2) }
<T, U> before its name?fun <T, U> pairToString(pair: Pair<T, U>): String {
return "${pair.first} and ${pair.second}"
}
fun main() {
val result = pairToString(Pair(10, "cats"))
println(result)
}fun <T, U> combine(a: T, b: U): T {
return b
}Box that can hold two different types of items, T and U. Which of the following class declarations correctly uses multiple type parameters and allows storing and retrieving both items?Pass the test and this pattern moves unknown