Is there a way to pass the right hand side of `{So...
# android
a
Is there a way to pass the right hand side of
{SomeObject} is {SomeClass}
as a parameter? I am trying to perform type checking inside a function where the type to check for meeds to be passed as an argument. Any way to achieve this?
i
You can pass someClass: KClass<*> and use it
someClass.isInstance({SomeObject})
a
That syntax doesn't seem right. And I have already tried with KClass but the parameter is not usable as RHS of
is
keyword.
e
if you want to use a "variable" RHS to
is
, it must be a reified type parameter to an inline function
but for most purposes, I recommend that you write a function taking
fun f(t: KClass<T>)
and leave
inline fun <reified T: Any> f() = f(T::class)
as a convenience wrapper only
yes,
isInstance
is different syntax than
is
, but
inline
should be used judiciously to avoid excessive duplication of bytecode
a
I am just trying to improve readability by shifting the type check behind an appropriately named function. The inline function will only have type check and function invocation if it fails.
Do you think it is worth it?
e
sure, just my recommendation and that sounds fine