Private visibility modifier and subpackages

So, I recently started experimenting with Kotlin, and I came across this:

If a top-level ad is marked as closed, it is closed for the package declared in ( see Visibility modifiers ). Since the packages really nest in Kotlin, that is, the package foo.bar is considered a member of foo, if something personal is in the package, it is visible to all subpackages.

Note that members of external packages are not imported by default, i.e. in the file in the package foo.bar, we cannot access foo members without importing them. From: Visibility and Package Placement

So, consider the following example:

File1.kt

package foo private fun bar() = println("This is bar!!!") 

and File2.kt

 package foo.baz import foo.bar fun main(args: Array<String>) = bar() 

From what I understand, the function bar () should be visible in the package foo.baz and, therefore, can be called from main (). But when I try to compile above, I get the following error message:

Error: Kotlin: cannot access the bar: this is 'private' in 'foo'

Is this a bug or has the language specification been updated and there is no documentation? Did I miss something?

Thanks in advance.

+5
source share
2 answers

We recently changed the visibility rules so that packets no longer nest. So this is not a mistake in the compiler, but in the documents

+6
source

Although it may be, as Andrei Breslav wrote, that the rules have changed, you can still use your code even with versions 0.12.1218 and 0.12.200.

Since the rules have been changed, you should not do this, but if you really want to do this, just change the import statement: import foo.* Instead of importing the string explicitly.

-1
source

All Articles