CastUtils

@API(status = API.Status.EXPERIMENTAL, since = "4.1.0")
object CastUtils

Functions

Link copied to clipboard
@JvmName(name = "castOr")
inline fun <T : Any> castOr(obj: Any?, kClass: KClass<T>, default: T): T

Casts the given obj to T or returns default if the cast fails.

Link copied to clipboard
@JvmName(name = "castOrElse")
inline fun <T : Any> castOrElse(obj: Any?, kClass: KClass<T>, default: () -> T): T

Casts the given obj to T or returns the default if the cast fails.

Link copied to clipboard
@JvmName(name = "castOrThrow")
inline fun <T : Any> castOrThrow(obj: Any?, kClass: KClass<T>): T

Casts the given obj to T or throws a TypeCastException if the cast fails.

Link copied to clipboard
@JvmName(name = "safeCast")
inline fun <T : Any> safeCast(obj: Any?, kClass: KClass<T>): T?

Casts the given obj to T or returns null if the cast fails.

Link copied to clipboard
@JvmName(name = "withCast")
inline fun <T : Any, R> withCast(obj: Any?, kClass: KClass<T>, block: T.() -> R): R?

Casts the given obj to T and triggers the block if successful.