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?infix 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 workNothing::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 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 isAssignableFrom
ClaudiuB
01/17/2018, 8:44 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)
}
nestedClass
, 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?
anymoreInt : Number
, i hope sokevinmost
01/17/2018, 8:50 PMKClass<Number>
is an instance of Int
Int::class.isInstance(3)
would be trueInt::class.isInstance(Number::class)
would notKClass::class.isInstance(Number::class)
would be trueNumber::class
is a KClass<Number>
, which makes it an instance of KClass
ClaudiuB
01/17/2018, 8:52 PMkevinmost
01/17/2018, 9:00 PMsuperTypes
recursively maybeallSupertypes
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!KClass#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)
}
https://i.imgur.com/uLNxG3Z.png▾
mwerschy
01/17/2018, 9:21 PMClaudiuB
01/17/2018, 9:23 PMmwerschy
01/17/2018, 9:26 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 as
ClaudiuB
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::class
kevinmost
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 KType
// 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