ClaudiuB
01/17/2018, 8:37 PMas? or as doesn't seem to work when using them with generic T: KClass<out Any>. For example someClass as T will return someClass, even when T is not `someClass`'s ancestor. if anyone could put an eye on the code in the comment to this, that would be great! Currently, in trying to achieve instanceof-like behavior, my method returns true for Nothing::class instanceof Number::class and I wish I was joking 🤖 . I've been trying to figure this out for maybe 3 hours now?If I'm using generics properly, then as and as? are broken when used with generics?ClaudiuB
01/17/2018, 8:37 PMinfix fun <PossibleSuperClass> KClass<out Any>.instanceof(clazz: PossibleSuperClass)
: Boolean where PossibleSuperClass : KClass<out Any> {
return try {
this as PossibleSuperClass
true
} catch (e: ClassCastException) {
false
}
}
Same happens if i go the as? way. My hunch is there's issues with PossibleSuperClass.
When debugging, `this`/`$receiver` is java.lang.Void, clazz is kotlin.Number, and this as PossibleSuperClass returns `this`s value, java.lang.void in this case (???). The value of this is returned every time even when an exception should be thrown or null should be returnedcedric
01/17/2018, 8:40 PMClaudiuB
01/17/2018, 8:40 PMcedric
01/17/2018, 8:40 PMreified. But the code you pasted above is scarykevinmost
01/17/2018, 8:40 PMNothing::class is Number::class wouldn't even workkevinmost
01/17/2018, 8:41 PMNothing::class is Number which will return false, because you're checking if a KClass object is an instance of Number, which it isn'tClaudiuB
01/17/2018, 8:41 PMClaudiuB
01/17/2018, 8:41 PMkevinmost
01/17/2018, 8:42 PMis or instanceof. In Java that's something like Foo.class.isAssignableTo(Bar.class) I believecedric
01/17/2018, 8:42 PMisAssignableFrom is probably what you wantkevinmost
01/17/2018, 8:42 PMClaudiuB
01/17/2018, 8:42 PMkevinmost
01/17/2018, 8:42 PMcedric
01/17/2018, 8:42 PMisAssignableFrom, just reverse the argumentsClaudiuB
01/17/2018, 8:43 PMisAssignableFrom an hour and something agocedric
01/17/2018, 8:43 PMvalue::class.java.isAssignableFrom...kevinmost
01/17/2018, 8:44 PMKClass like isAssignableFromClaudiuB
01/17/2018, 8:44 PMClaudiuB
01/17/2018, 8:45 PMcedric
01/17/2018, 8:45 PMClaudiuB
01/17/2018, 8:45 PMinfix fun <PossibleSuperClass> KClass<out Any>.instanceof(clazz: PossibleSuperClass)
: Boolean where PossibleSuperClass : KClass<out Any> {
return clazz.nestedClasses.contains(this)
}ClaudiuB
01/17/2018, 8:45 PMnestedClass, like nested inside the inheritance hierarchy of the classkevinmost
01/17/2018, 8:46 PMClaudiuB
01/17/2018, 8:48 PMinfix fun <PossibleSuperClass> KClass<out Any>.instanceof(clazz: PossibleSuperClass)
: Boolean where PossibleSuperClass : KClass<out Any> {
return clazz.isInstance(this)
}kevinmost
01/17/2018, 8:49 PMInt::class instanceof Number::class to return true there?ClaudiuB
01/17/2018, 8:49 PMas or as? anymoreClaudiuB
01/17/2018, 8:49 PMInt : Number, i hope sokevinmost
01/17/2018, 8:50 PMKClass<Number> is an instance of Intkevinmost
01/17/2018, 8:50 PMInt::class.isInstance(3) would be truekevinmost
01/17/2018, 8:50 PMInt::class.isInstance(Number::class) would notkevinmost
01/17/2018, 8:50 PMKClass::class.isInstance(Number::class) would be truekevinmost
01/17/2018, 8:50 PMNumber::class is a KClass<Number>, which makes it an instance of KClassClaudiuB
01/17/2018, 8:52 PMkevinmost
01/17/2018, 9:00 PMsuperTypes recursively maybekevinmost
01/17/2018, 9:00 PMallSupertypes that flattens out the hierarchy for you into one list, but it's JVM-only, hahaClaudiuB
01/17/2018, 9:10 PMsuperTypes, thanks for the idea. Maybe I can figure it out without JVM after all!ClaudiuB
01/17/2018, 9:18 PMKClass#starProjectedType which returns KType, which has isSubtypeOf. Method is
infix fun <PossibleSuperClass> KClass<out Any>.instanceof(clazz: PossibleSuperClass)
: Boolean where PossibleSuperClass : KClass<out Any> {
return this.starProjectedType.isSubtypeOf(clazz.starProjectedType)
}ClaudiuB
01/17/2018, 9:19 PMhttps://i.imgur.com/uLNxG3Z.pngâ–¾
mwerschy
01/17/2018, 9:21 PMClaudiuB
01/17/2018, 9:23 PMClaudiuB
01/17/2018, 9:25 PMmwerschy
01/17/2018, 9:26 PMmwerschy
01/17/2018, 9:27 PMClaudiuB
01/17/2018, 9:42 PMKotlin/Native is what I meant, forget about the VM thing. But Kotlin/Native would still have to implement stuff like KType and its functions, right?cedric
01/17/2018, 9:43 PMis and asClaudiuB
01/17/2018, 9:43 PMKType a Kotlin class used as an umbrella thing just for Kotlin + JVM together?kevinmost
01/17/2018, 9:46 PMKClass instance is a supertype of another, basicallyClaudiuB
01/17/2018, 9:46 PMx is y::class or through the type, it complains. if y : KType and try x is y it complains, it only works with Real Types, like OutOfMemoryException, not platformUiException::classkevinmost
01/17/2018, 9:46 PMis and as cover it when you know the types staticallyClaudiuB
01/17/2018, 9:46 PMstatic types better describes itkevinmost
01/17/2018, 9:47 PMClass<?> type = Foo.class;
if (myFoo instanceof type) { ... }
in Javacedric
01/17/2018, 9:47 PMkevinmost
01/17/2018, 9:48 PMmyKClass.isAssignableFrom(otherKClass), you have to go through the .java extension and get a regular Class instance for thatClaudiuB
01/17/2018, 9:48 PMKClass and KTypeClaudiuB
01/17/2018, 9:48 PM// e.g. object::class instanceof Activity::class
infix fun <PossibleSuperClass> KClass<out Any>.instanceof(clazz: PossibleSuperClass)
: Boolean where PossibleSuperClass : KClass<out Any> {
return this.starProjectedType.isSubtypeOf(clazz.starProjectedType)
}
// e.g. object instanceof Activity::class
infix fun <PossibleSuperClass> Any.instanceof(clazz: PossibleSuperClass)
: Boolean where PossibleSuperClass : KClass<out Any> {
return this::class instanceof clazz
}mwerschy
01/18/2018, 6:27 AMstarProjectedType which is JVM only. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect.full/star-projected-type.htmlClaudiuB
01/18/2018, 9:12 AMKClass#superTypes : List<KType> which we could use instead 🤔 Flatmap it to a boolean maybe?gildor
01/21/2018, 3:57 AM