Zhuinden / guide-to-kotlin

[GUIDE] This tutorial assumes all you know is Java, but you want to learn Kotlin.
https://github.com/Zhuinden/guide-to-kotlin/wiki
Apache License 2.0
1.29k stars 99 forks source link

Visibility modifiers: package visibility does exist #13

Closed homer-jay closed 5 years ago

homer-jay commented 5 years ago

In the visibility section, it says:

There is NO package visibility in Kotlin

This is actually not true, as explained in the official guide, in the section Visibility modifiers - package.

I was also confused by the explanation about internal. In the wiki it says that internal is not equivalent to package, and it is true, but I'd add that internal is "module" visibility (where modules are IntelliJ modules, Maven projects, gradle source sets, etc).

Zhuinden commented 5 years ago

internal is compilation module visibility, which means it is completely different from package visibility.

Therefore, Kotlin has the addition of internal visibility, but does not have package visibility.

Also internal gets quirky name in Java.

homer-jay commented 5 years ago

but does not have package visibility

OK, so strictly speaking it does not have Java's package-default visibility mechanism, but it allows for "top-level" members directly inside a package:

package foo

fun bar() { ... }

So I agree it's not the same as in Java, because in Kotlin bar would be accesible by any class in a different package (as long as you import it). But in some sense it fills that void, because top-level members can be used (without the need to import them) by classes in that same package. I thought Java programmers would probably appreciate reading about it.

Zhuinden commented 5 years ago

The problem isn't that you don't need to import it, it's that you can still import it elsewhere and see it.

The top-level declaration is essentially public. 😉

homer-jay commented 5 years ago

Ok then. That would be like import static. Closing.