Guys how do i change function to make it get `T` f...
# getting-started
a
Guys how do i change function to make it get
T
from the
klass
?
Copy code
fun <T> getProperty(klass: KClass<*>): T? {
        return properties.find { it::class == klass } as T?
    }
Right now it's working as
entity.getProperty<String>(String::class)
and i want
entity.getProperty(String::class)
n
It should work if you assign it to a variable of type String. Otherwise it cannot infer the correct type.
a
Can i make it infer same type as klass?
d
fun <T : Any> getProperty(klass: KClass<T>)
Alternatively (or additionally if you want):
inline fun <reified T : Any> getProperty()
, then you can just do
T::class
inside the function.
a
Oh, you saved my day! Didn't find that
<T : Any>
in generics docs
n
Will
T::class
work despite type erasure?
a
T::class
works with reified inline func
But i cannot use inline func because some data inside is private
n
That’s cool in comparison with Java
a
Yes, that's a nice thing
s
You can have a function that takes
KClass
as a parameter and an inline function that calls that