asNullable

inline fun <T, E : T> T.asNullable(orThrow: Boolean = false): E?

Safely casts this object to the specified type E, returning null if the cast fails.

Return

The casted object of type E, or null if cast fails and orThrow is false.

Parameters

orThrow

If true, throws IllegalArgumentException when cast fails; if false, returns null.

Type Parameters

E

The target type to cast to. Must be a subtype of T.

Throws

if orThrow is true and the cast fails.

Samples