是否有可能编写一个返回通用类型的函数?像这样:
fun <T> doSomething() : T {
when {
T is Boolean -> somethingThatReturnsBoolean()
T is Int -> somethingThatReturnsInt()
else -> throw Exception("Unhandled return type")
}
}
inline fun <reified T> doSomething() : T {
return when (T::class.java) {
Boolean::class.java -> somethingThatReturnsBoolean() as T
Int::class.java -> somethingThatReturnsInt() as T
else -> throw Exception("Unhandled return type")
}
}
但是你还必须通过未经检查的转换,使编译器相信T是布尔类型或整型,就像示例中一样。
reified
可以让真实的T类型可访问,但只能在内联函数内使用。如果你想要一个普通函数,你可以尝试:
inline fun <reified T> doSomething() : T = doSomething(T::class.java)
fun <T> doSomething(klass: Class<T>): T {
return when (klass) { ... }
}
somethingThatReturnsInt()
)传递给通用函数,并从传递的函数本身推断返回类型。以下是Kotlin
示例:// This function takes any method you provide and infers the result-type
public fun<T> generic_function(passed_function: () -> T): T? =
passed_function()
// Pass your function. The return type will be inferred directly from somethingThatReturnsInt()'s return-type
val res: Int? = generic_function(::somethingThatReturnsInt)
// For the sake of completeness, this is valid as well
val res: Int? = generic_function<Int?> { somethingThatReturnsInt() }
data class Coffee(val milk: Double, val sugar: Int)
class ComparingBuilder(val oldInstance: Coffee, val updatedInstance: Coffee) {
operator fun <T> invoke(accessor: Coffee.() -> T?): T? =
if (whatever) { oldInstance.accessor() }
else { updatedSupplier.accessor() }
}
fun useComparingBuilder(
oldInstance: Coffee,
updatedInstance: Coffee,
) = OptionalBuilder(oldInstance, updatedInstance).let { builder ->
val remixedCoffee = Coffee(
milk = builder { milk },
sugar = builder { sugar },
)
}
简而言之,这不是制作通用lambda的方法,但它是一种可以使用某些东西就像它是通用lambda的方法。
很难用语言表达清楚,希望代码更加清晰明了....
Any
可能是你想要使用的东西。它是 Kotlin 类层次结构的根。
同时,Any?
语法也可用于 Any
。
fun doSomething(key: Any): Any {
when {
key is Boolean -> return true
key is Int -> return 1
else -> throw Exception("Unhandled return type")
}
}
return
语句。 - Miha_x64