intellij-ideakotlinkotlinc

Is there a way of limiting stdlib functions' visibility in Kotlin?


Kotlin comes with many standard library functions, a lot of which are visible by default without actually importing them. So functions like listOf, arrayListOf, ArrayList<T>(), ... are de facto inserted into global namespace. Is there a compiler flag that would disable this behavior?


Solution

  • Sadly, you can't. just as you can't prohibit the auto-imported java.lang package in java.

    You don't need to worry about the auto-imported top-level functions. If you don't use any top-level functions, there is no Class Reference/Method Reference emitted to the Java byte code. And all of the *arrayOf functions will be transformed to Java array creation, for example:

    Kotlin          |        Java
    ------------------------------------
    byteArrayOf     |    new byte[]
    ------------------------------------
    shortArrayOf    |    new short[]
    ------------------------------------
    intArrayOf      |    new int[]
    ------------------------------------
    longArrayOf     |    new long[]
    ------------------------------------
    floatArrayOf    |    new float[]
    ------------------------------------
    doubleArrayOf   |    new double[]
    ------------------------------------
    charArrayOf     |    new char[]
    ------------------------------------
    booleanArrayOf  |    new boolean[]
    ------------------------------------
    arrayOf<T>      |    new T[]
    ------------------------------------