https://kotlinlang.org logo
#announcements
Title
# announcements
u

U75957

06/21/2019, 10:56 AM
this::class
is available, but
super::class
- not. Need this.
k

kralli

06/21/2019, 11:02 AM
super
references any super type, that is the class and interfaces. It is not specific to a class, making it impossible to provide such a function. If you want to get hold of all super types, use
this::class.supertypes
.
Also
this::class.superclasses
if you want the classes only.
m

marstran

06/21/2019, 11:05 AM
There is also
this::class.allSuperclasses
u

U75957

06/21/2019, 11:12 AM
@kralli @marstran thank you, guys. But how can we get just direct parent class? I use Kotlin/JS, not JVM.
m

marstran

06/21/2019, 11:18 AM
I don't think you can. What do you need that for?
Also, let's say you have
class A : A1, A2, A3
. Which class object would you expect to get?
u

U75957

06/21/2019, 11:25 AM
@marstran I interest only in class. Only one class can apear in a supertype list. So in your example I expect
Any
But in such example
class A : A1, A2(), A3
I expect
A2
this::class.supertypes
is marked as supported for JS in the documentation. But in fact compiler produce error
Unsupported [This reflection API is not supported yet in JavaScript]
k

kralli

06/21/2019, 11:28 AM
this::class.superclasses
is marked as supported for JVM only,
this::class.supertypes
however should be supported
m

marstran

06/21/2019, 11:29 AM
Anyway, this kind of feels like an XY problem. What exactly are you trying to solve? If your application design requires you to find the direct superclass of your object, you must be doing something wrong.
1
1
u

U75957

06/21/2019, 11:46 AM
@marstran easy to say "you must be doing something wrong". It can be said about reflection API at all. My application design requires to interact with external JS lib. To make my code work I need to write some boilerplate (because Kotlin/JS do it bad). Every time I extend any TypeScript class, I need to call some function with parameter of parent class (because Kotlin/JS can't inherit TypeScript class correctly). Of cause I do
A2::class.js
. I just wanted to bring this duplicate code to a separate function.
m

marstran

06/21/2019, 12:09 PM
Sorry, didn't mean to be condescending. What I meant was that your actual problem is extending a TypeScript class, not getting the class object of the direct superclass. You might get better answers by asking that instead, and giving the kotlin.reflections code as an example of what you have tried. 🙂
2 Views