https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
g

GarouDan

03/30/2019, 10:50 AM
Hey guys, let me ask you something I would like to put a code like this one
fun getPackage(class_: KClass<*>): String? = class_.qualifiedName?.replace(".${class_.simpleName}", "")
in my
common
module of a
multiplatform
project. The problem is the
.qualifiedName
part doesn’t work in JavaScript and because of that I’m receiving the following error:
Unsupported [This reflection API is not supported yet in JavaScript]
It would be very nice to see JavaScript supporting the full reflection API, but, either way, the problem is a bit different. My code is not compiling in the JavaScript module because of that. But all of my targets other than the js could benefit from that, so, how could I avoid this error in order to make this code work for native and jvm targets and let js without that? Where the code could live inside the
common
module?
i

ian.shaun.thomas

03/30/2019, 11:44 AM
you can add a sourceset that everything but JS depends on. This is a easy way to add that code to everything that does benefit. https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#connecting-source-sets
d

Dico

03/30/2019, 6:08 PM
Would it not be an option to make an
expect val KClass<*>.qualifiedName: String
?
i

ian.shaun.thomas

03/30/2019, 6:11 PM
if he's ok with the method appearing but always returning null (or whatever else) then sure that's another option.
g

GarouDan

04/01/2019, 11:12 AM
@ian.shaun.thomas I was thinking about that before, I agree, I could move follow with that but it’s a little weird. I would prefer to hardcode the js package name instead.
@Dico, I’m almost certain that this will not work since the reflection options are checked in compile time by this class https://github.com/JetBrains/kotlin/blob/ba6da7c40a6cc502508faf6e04fa105b96bc7777/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsReflectionAPICallChecker.kt At least I tried some similar attempt before but I had no success
d

Dico

04/01/2019, 1:52 PM
@GarouDan As the code suggests, that class is not catching extension properties or functions, as it ensures the containing class is a subtype of KClass. However, it might be good to use a different name nonetheless.