LeoColman
04/30/2019, 3:27 PMinline fun <reified T> foo() {
when(T) {
is Foo -> tFoo()
is Bar -> tBar()
}
}
streetsofboston
04/30/2019, 3:29 PMT::class
and go from there (it will return a KClass<out T>
value)Cody Engel
04/30/2019, 3:29 PMinline fun <T> blah(thing: T) {
when (thing) {
is String -> ""
is Int -> thing + 1
}
}
Cody Engel
04/30/2019, 3:30 PMstreetsofboston
04/30/2019, 3:31 PMfoo<MyBar>()
or something similar.Cody Engel
04/30/2019, 3:33 PMfun <T> T.blah() {
when (this) {
is String -> ""
is Int -> this + 1
}
}
Would that be in the same spirit? Sorry might be missing what the usecase would look likeLeoColman
04/30/2019, 3:33 PMT
LeoColman
04/30/2019, 3:33 PMgetFactory<Foo>
streetsofboston
04/30/2019, 3:33 PMCody Engel
04/30/2019, 3:34 PMstreetsofboston
04/30/2019, 3:36 PMT::class.java.isAssignableFrom(Class<*>)
LeoColman
04/30/2019, 3:36 PMLeoColman
04/30/2019, 3:37 PMT::class
only?streetsofboston
04/30/2019, 3:38 PMKClass<*>.supertypes
to write your own KClass<*>.isA(KClass<*>)
extension function…LeoColman
04/30/2019, 3:42 PMLuke
04/30/2019, 3:50 PMinline fun <reified T: A> foo() {
when(T::class) {
B::class -> b()
C::class -> c()
}
}
Luke
04/30/2019, 3:52 PMLeoColman
04/30/2019, 3:53 PMstreetsofboston
04/30/2019, 4:10 PMLeoColman
04/30/2019, 4:11 PMStephan Schroeder
04/30/2019, 4:15 PMinline fun <reified T> factory(): Factory<T> = when(T::class) {
Int::class -> object : Factory<Int>{override fun create()=1}
Double::class -> object : Factory<Double>{override fun create()=2.0}
else -> throw IllegalArgumentException("unknown class")
}
interface Factory<out G> {
fun create():G
}
alas it doesn’t https://pl.kotl.in/r0zfH3cm_LeoColman
04/30/2019, 4:27 PMFactory<Any>
. InterestingLeoColman
04/30/2019, 4:27 PM