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

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

1 Like

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

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)
}

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)
}