Why not provide namespace keyword like C++/C#


#1

I’m new to Kotlin. Why does Kotlin not provide the namespace keyword like C++/C#?


#2

In Kotlin packages are the closest analog of C# namespaces. You can define package with the package keyword in the beginning of the file.


#3

One follow-up question. Can I use function reference to the methods defined in another package? I think Kotlin should learn a good lesson from C about naming conflicts for those functions defined in the default package.

For example,

In a Utils.kt

package utils

fun utilMethod(x: Int): Int= …

In the Main.kt

fun callUtilMethod(x: Int, fn:(Int) -> Int): Int {
return fn(x)
}

fun main(…) {
callUtilMethod(10, ::utils.utilMethod)
}


#4

As with a simple function call across packages, you should import the function first and then reference it with a simple name:

import utils.utilMethod

...

fun main() {
    callUtilMethod(10, ::utilMethod)
}