Top-level private inconsistency

If I have a bit of top-level code like this:

//File1.kt

package tech.foo

private object LOG : Logger by LoggerFactory.getLogger(LOG::class.java)

And similar code in File2

//File2.kt
package tech.foo

private object LOG : Logger by LoggerFactory.getLogger(LOG::class.java)

I get a redeclaration error.

However, I can write this top-level declaration line in two different files:

private val cassandra = Guice.createInjector(ModuleCassandra())

Thoughts?

Currently it works the way it does because a top-level val becomes a member of a class with the name derived from the file name, and an object gets compiled into a class with the name derived from the package and the name of the object itself. So the two properties become fields in two different classes, and the two objects become two classes with the same qualified name.

This may change in the future, though; there’s an issue for this problem.

2 Likes

Always on it!
I appreciate it.

To resolve this issue, I just placed common code into a single File, and just re-used them elsewhere.