Consider providing KSDeclaration.isPlatformCode()
- Dominant language
- Kotlin
- Stars
- 3.5k
- Forks
- 415
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 53
Description
I'm generating code with KSP. The generated code will be used in common/intermediate code.
I understand that this is not allowed.
See https://slack-chats.kotlinlang.org/t/16366233/i-m-trying-out-kotlin-2-0-beta-3-and-it-looks-like-generated:
Common/intermediate (= none-platform) code cannot reference generated code in the compilation of platform code.
Generated codes are treated as platform code (you'll have to use expect/actual).
I therefore implemented the proposed expect/actual solution.
Everything works fine (see https://github.com/softappeal/yass2/blob/main/yass2-generate/src/jvmMain/kotlin/ksp/Generate.kt).
For finding out if a `KSDeclaration` is in platform specific code I use the following code:
```kotlin
private val Platforms = setOf(
"jvm", // JVM
"js", // JavaScript
"wasmJs", "wasmWasi",// WebAssembly
"macosX64", "macosArm64", // macOS
"iosArm64", "iosX64", "iosSimulatorArm64", // iOS
"linuxX64", "linuxArm64", // Linux
"watchosArm64", "watchosX64", "watchosSimulatorArm64", "watchosDeviceArm64", // watchOS
"tvosArm64", "tvosX64", "tvosSimulatorArm64", // tvOS
"mingwX64", // Windows
)
public fun KSDeclaration.isPlatformCode(): Boolean {
val filePath = containingFile!!.filePath
return Platforms.any { platform -> filePath.contains("/${platform}Main/") || filePath.contains("/${platform}Test/") }
}
```
`isPlatformCode()` is then used to find out if a generated function needs the `actual` keyword.
I believe/hope there must be a more elegant solution for `isPlatformCode()`.
Any ideas are welcome.
Or even better: Could `isPlatformCode()` be part of KSP API?
That would be very convenient for implementing `expect/actual `solutions.
Contributor guide
Assessment
This issue has not been assessed yet.