10

Official docs on visibility modifiers in Kotlin say that package-level elements marked private are be visible only in the module in which they are declared.

So class A declared in Module1.kt isn't visible in Module2.kt. But if I try to add to Module2.kt it's own class A I get the Redeclaration: A error.

Since I can't access in Module2.kt to Module1's A class, why isn't the name A free to use?

netimen
  • 4,199
  • 6
  • 41
  • 65
  • 1
    You're probably confused with what modules are, please check that your `Module1.kt` and `Module2.kt` are actually in different modules according to [this question](http://stackoverflow.com/questions/35271413/what-is-a-kotlin-module). I tried this with multiple modules and didn't have a redeclaration error. – hotkey Feb 15 '16 at 13:43

1 Answers1

15

"A module is a set of Kotlin files compiled together" (Visibility Modifiers - Kotlin Programming Language).

In your example, Module1.kt and Module2.kt are separate source files and despite their names they are not necessarily part of separate modules:

  • If they are compiled together then they are part of the same module.
  • If they are compiled separately from one another then they will be part of different modules and each can define their own private class A.

Keep in mind that visibility is different from identity. Even if a class is not visible elsewhere it doesn't mean that it does not exist. Loading multiple class declarations with the same fully-qualified name can (and likely will) cause issues at run-time.

mfulton26
  • 29,956
  • 6
  • 64
  • 88
  • 3
    Seems like this makes `private` kind of useless, since it's effectively `internal`. I ran into this when trying to create a file-scoped helper class. – frodo2975 Jun 27 '22 at 20:28