Understanding modules

Hello.

I’m reading the kotlin reference about visibility modifiers. There are a lot of references to the term “module” but I couldn’t find anything about what the module is.
There are some information on the “http://confluence.jetbrains.com/”. It starts from the fact that modules are beeing redesigned now.

Are there any docs on “modules”?
How can feel the difference between internal and public modifiers?

Regards, Alexey

1 Like

Modules are those things you break a project into at design time. Kotlin module maps one-to-one to IntelliJ's module (iml-file). Kotlin M8 does not respect modules yet (M9 will), so you can't see the difference between public and internal yet, but it will come :) Sorry for the inconvenience

1 Like

Kotlin module maps one-to-one to IntelliJ's module (iml-file).

And what about comand line compilation or a maven project (if there are no any IDE specific files inside)?

Each compiler run, by default, is a separate module: all the binary dependencies will be treated as being not in the module being compiled at the moment.

Got it! Thanks!

Does this mean that the internal modifier is only checked by the compiler but there are no guarantees at runtime?

Where possible the system does name mangling to make it harder to use it invalidly. But remember that at runtime there are no guarantees even for private members (you can open them up using reflection). Accidental usage from Java is prevented though.

This helped me from an Android Development point of view:

A module is a set of Kotlin sources compiled together:

  • an IntelliJ IDEA module;
  • a Maven project;
  • a Gradle source set (with the exception that the test source set can access the internal declarations of main);
  • a set of files compiled with one invocation of the Ant task.
    (from the Kotlin Reference Documentation)

That means that if you have different build flavours in your gradle configuration resulting in different source sets (for production and debug versions for example) , then an internal class from one source set wouldn’t be available to be used in another source set.

That’s my take from the topic.

1 Like

I am trying to figure out “modules” yet, when it says a sourced compiled together, in the case is a maven project, does it mean the WHOLE project? then for me that is public inside that “module”.

The concept of packapge-private is very clear for me since I can see the package on my project, but not the module, could someone please explain me?

A module is a concept in gradle. It is all files that will be compiled together to go into the same Jar file. As a shortcut (not 100% correct), you may think of it as a single source folder (with however many packages you have). Intellij/Android studio display the code per module as well.