i have this function: ```inline fun <reified T:...
# random
n
i have this function:
Copy code
inline fun <reified T: FeatureSet> get(clazz: KClass<T> = T::class): T?
when i call it like so
val a: Any? = foo.get()
i get the class
FeatureSet
which is abstract is there any way to forbid this syntax other than crashing at runtime ?
stackoverflow 1
r
If you get
FeatureSet
that means foo is FeatureSet also, right?
n
foo is the enclosing class,
ClassSet
although that should be irrelevant for the type inference here
r
Sorry for not actually helping. I'm confused. Is it
Copy code
inline fun <reified T: FeatureSet> get(clazz: KClass<T> = T::class): T?
or
Copy code
inline fun <reified T: FeatureSet> T.get(clazz: KClass<T> = T::class): T?
If it's the first one as you typed, where does T get evaluated in
foo.get()
?
n
tinkered around a bit more seems like
Copy code
inline fun <reified T: Any> get(): KClass<T> = T::class
cannot be called without type parameter, because type inference fails if the signature changes to these however..
Copy code
inline fun <reified T: Any> get(): T
inline fun <reified T: Any> get(): T?
it seems like you can call it anyways (and it passes a wrong?
T
) it is the former T is getting evaluated via
T::class
but .. does that affect the type inference ?
seems like
Result<T>
or any other wrapping things also help.. but i'd prefer to not create unneccesary wrapping objects just to have type inference work this way..