Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An internal function in Kotlin is a function whose visibility is restricted to the module in which it is declared. The internal visibility modifier ensures that the function can be accessed by any code within the same compiled module, regardless of the package structure, but remains strictly invisible to code residing in external modules. In Kotlin, a “module” is defined as a set of Kotlin files compiled together. This specifically translates to:
  • An IntelliJ IDEA module.
  • A Maven project.
  • A Gradle source set (with the exception that the test source set can access the main source set’s internal declarations).
  • A set of files compiled with a single invocation of the <kotlinc> Ant task.
// Top-level internal function
internal fun computeHash(data: ByteArray): String {
    return data.joinToString("") { "%02x".format(it) }
}

class DataProcessor {
    // Class-level internal function
    internal fun parseConfiguration() {
        // Implementation details
    }
}
Core Mechanics and Compilation Details:
  • Package Independence: Unlike Java’s default “package-private” visibility, Kotlin’s internal modifier does not care about package boundaries. An internal function in com.example.network can be freely called by a class in com.example.ui, provided both packages are compiled within the same module.
  • Class Member Visibility: If an internal function is declared inside a class, the caller must also have visibility access to the class itself. For example, an internal function inside a private class is only accessible within the file where the private class is defined.
  • JVM Bytecode Representation: Because the Java Virtual Machine (JVM) lacks a direct equivalent to Kotlin’s module-level visibility, the Kotlin compiler translates internal functions into public methods in the generated Java bytecode.
  • Name Mangling: To prevent external Java code from accidentally invoking these compiled public methods, the Kotlin compiler applies name mangling. It appends a dollar sign and the module name to the function’s bytecode signature (e.g., parseConfiguration$my_module_name). If external Kotlin code attempts to call this mangled function, the Kotlin compiler will reject it during the compilation phase, enforcing the module boundary.
Master Kotlin with Deep Grasping Methodology!Learn More