Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of internal visibility modifier in Kotlin

I have a problem understanding the internal access modifier. In my examples I could not produce any situation where it behaves different then public. Even if I create a jars I can access the internal functions in the jar from outside the jar (from a different package unrelated to the one used in the jar). In the documentation the term module is used but I could not find out what a module is exactly.

My questions thus are:

  1. what is a module in kotlin?

  2. how does the access modifier internal differs from the public modiefier?

like image 666
Markus Knecht Avatar asked Sep 12 '14 17:09

Markus Knecht


People also ask

What is internal modifiers in Kotlin?

Internal Modifier Internal is a new modifier available in Kotlin that's not there in Java. Setting a declaration as internal means that it'll be available in the same module only. By module in Kotlin, we mean a group of files that are compiled together.

What are the different Kotlin scope modifiers?

There are four visibility modifiers in Kotlin: private , protected , internal , and public . The default visibility is public . On this page, you'll learn how the modifiers apply to different types of declaring scopes.

What is the use of internal in Kotlin?

You can have multiple Gradle modules that depend on each other within a single Android application, in that case, internal restricts visibility to within a given module.

What is the difference between public/private and internal modifiers in Kotlin?

The public modifiers means that the declarations are visible everywhere. In Kotlin the default visibility modifier is public while in Java is package-private. This modifier does not exist in Kotlin.


1 Answers

The Kotlin compiler version M8 (0.8.11) ignores modules, more precisely, it works as if all your code were put into the same module, this is why you can't get a visibility error for internal.

Starting with the next milestone release, every IDE module you create has its own visibility scope, and when you try to access an internal declaration from one module in another, you get an error. Publics, on the other hand, are visible across the whole project.

UPDATE: This has not been turned on in M8, postponed for later

UPDATE: This has been turned on since later Kotlin milestones and betas

like image 176
Andrey Breslav Avatar answered Sep 27 '22 20:09

Andrey Breslav