I am migrating a Java codebase to Kotlin, and I have a Java class with the following quite common structure:
class A {
// public static factories
// private constructors
// public A methods
// private A methods
// private static helpers
}
My problem is what to do with the private static helpers.
In Kotlin, if I put them as file-level private functions:
class A private constructor(...) {
companion object {
// factories
}
// A methods
// private A methods
}
// private helpers
Then when using the code from Java there will be a new AKt
class in the package, with no methods but still visible (and appearing as suggestion in the IDE). What was nicely isolated before now "leaks" as a new unintended public class into the API.
Having the private static helpers in the companion object is not really the same, because now the "logical" organization of the class is all "backwards" with the private, unimportant helpers being at the top:
class A private constructor(...) {
companion object {
// factories
// private helpers
}
// A methods
// private A methods
}
Is there a better alternative? Some way to hide the AKt
generated class maybe?
Edit: I though that the AKt
class had a public constructor because IntelliJ doesn't mark this as an error when editing the file:
AKt a = new AKt();
Not in the same project, and not in a client.
But when building the project, or when compiled with Gradle, it does fails with:
error: cannot find symbol
AKt mk = new AKt();