我有课
class Generic<T : SuperType>()
Run Code Online (Sandbox Code Playgroud)
这段代码不对
fun typeCheck(s: SuperType): Unit {
when(s){
is T -> //do some thin
}
}
Run Code Online (Sandbox Code Playgroud)
但是施放s类型T s as T显示警告 - 不安全的施法.
怎么检查s是什么类型T?
Kir*_*man 30
如果您需要检查某些内容是否属于泛型类型T,则需要有一个Class<T>要检查的实例.这是Java中常见的技术,但在Kotlin中我们可以使用内联工厂方法来获取类对象.
class Generic<T : Any>(val klass: Class<T>) {
companion object {
inline operator fun <reified T : Any>invoke() = Generic(T::class.java)
}
fun checkType(t: Any) {
when {
klass.isAssignableFrom(t.javaClass) -> println("Correct type")
else -> println("Wrong type")
}
}
}
fun main(vararg args: String) {
Generic<String>().checkType("foo")
Generic<String>().checkType(1)
}
Run Code Online (Sandbox Code Playgroud)
小智 8
I know that I'm kinda late to this thread, but I just want to recap on the answer provided by Alexander Udalov.
It is, indeed, impossible to determine the type of a generic parameter in Kotlin unless you're using inline functions and declaring the generic type as reified.
Not sure if I'll be able to answer this question entirely and accurately, but I feel like my contribution might still be valuable for someone who is attempting to do just that. So let's say you have a few data classes, and you want to check which type you're dealing with.
You could use a function like that:
inline fun <reified T> checkType() = when (T::class) {
TypeA::class -> println("TypeA")
else -> println("Type not recognized")
}
Run Code Online (Sandbox Code Playgroud)
however, functions that call it must also be inline, so you might have to write something like
inline fun <reified T> someOtherFunction(data: T) {
checkType<T>
}
Run Code Online (Sandbox Code Playgroud)
however, if you cannot allow for an inline function (let's say in an interface!), you can kinda 'cheat' the system by saying, for example
class AmazingTypes {
inline fun <reified T> checkType(genericParameter: T) = when (T::class) {
TypeA::class -> println("TypeA")
else -> println("Type not recognized")
}
}
fun myAwesomeMethod(someParameter: Any) {
val amazingClass = AmazingClass()
amazingClass.checkType(someParameter)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13573 次 |
| 最近记录: |