Ray Rahke
02/24/2024, 4:40 AM::fn
. Is there a reason why this wasn't chosen as the notation for referencing a class itself rather than an instance, as opposed to having to do Foo::class
?Youssef Shoaib [MOD]
02/24/2024, 4:46 AM::Foo
references `Foo`'s constructorYoussef Shoaib [MOD]
02/24/2024, 4:46 AMnew
keyword)Ray Rahke
02/24/2024, 5:23 AMRay Rahke
02/24/2024, 5:24 AMval thing = Foo()
fun<T: Foo> isInstance(ctor: () => T) {
return thing is ctor
}
isInstance(::Foo)
Ray Rahke
02/24/2024, 5:24 AMRay Rahke
02/24/2024, 5:27 AMreturn thing::class.constructor is ctor
Ray Rahke
02/24/2024, 5:27 AMYoussef Shoaib [MOD]
02/24/2024, 5:35 AM::class
then:
inline fun <reified T> Any?.isInstance() = this is T
val thing = Foo()
thing.isInstance<Foo>()
Ray Rahke
02/24/2024, 5:56 AMRay Rahke
02/24/2024, 5:56 AMRay Rahke
02/24/2024, 5:56 AMJoffrey
02/24/2024, 10:53 AMmy real use case filters through a list and returns all who are instances
Then there is already
filterIsInstance<T>()
. If your use case slightly different, you can look at its implementationRay Rahke
02/24/2024, 11:11 AMRay Rahke
02/24/2024, 11:11 AMRay Rahke
02/24/2024, 11:11 AMRay Rahke
02/24/2024, 11:11 AMJoffrey
02/24/2024, 11:13 AMJoffrey
02/24/2024, 11:21 AMKClass
and write an inline reified wrapper on topJoffrey
02/24/2024, 11:22 AMKClass
and then a few inline reified wrappers taking 1, 2, 3, 4 type parameters.Joffrey
02/24/2024, 11:23 AM