Do not use qualifiedName property in KClass.cast function, because it's not supported in K/JS
Introduce an internal property that returns either qualified or simple name, and use it instead.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.reflect
|
||||
|
||||
internal actual inline val KClass<*>.qualifiedOrSimpleName: String?
|
||||
get() = simpleName
|
||||
@@ -0,0 +1,9 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.reflect
|
||||
|
||||
internal actual inline val KClass<*>.qualifiedOrSimpleName: String?
|
||||
get() = qualifiedName
|
||||
@@ -23,10 +23,13 @@ import kotlin.internal.LowPriorityInOverloadResolution
|
||||
@ExperimentalStdlibApi
|
||||
@LowPriorityInOverloadResolution
|
||||
fun <T : Any> KClass<T>.cast(value: Any?): T {
|
||||
if (!isInstance(value)) throw ClassCastException("Value cannot be cast to $qualifiedName")
|
||||
if (!isInstance(value)) throw ClassCastException("Value cannot be cast to $qualifiedOrSimpleName")
|
||||
return value as T
|
||||
}
|
||||
|
||||
// TODO: replace with qualifiedName when it is fully supported in K/JS
|
||||
internal expect val KClass<*>.qualifiedOrSimpleName: String?
|
||||
|
||||
/**
|
||||
* Casts the given [value] to the class represented by this [KClass] object.
|
||||
* Returns `null` if the value is `null` or if it is not an instance of this class.
|
||||
|
||||
Reference in New Issue
Block a user