Are namespaces coming to Kotlin? Really missing th...
# announcements
o
Are namespaces coming to Kotlin? Really missing that feature for organizing top level functions. As an alternative there are `object`s, but a function inside an object isn't enforced to be stateless..
c
What do you want from namespaces that you can't get from packages?
o
Package names are way too long.
com.mycompany.myapp.checkout.calculatePrice(orders)
vs
Checkout.calculatePrice(orders)
👍 1
r
Scala lets you import a package, so
Copy code
import com.mycompany.myapp.checkout

checkout.calculatePrice(orders)
👍 1
A related irritation is that if you want to use package as a namespace for a file you have to have one file per directory or IntelliJ whinges at you about the package statement not matching. I’ve raised https://youtrack.jetbrains.com/issue/KTIJ-17767 about this.
👍 1
e
Btw, top-level functions are not enforced to be stateless either. Not much different from object in this respect.
🤔 1
n
So far, I don't see a case for namespaces, I see a case for allowing importing packages so you can use qualified names instead of unqualified
Many languages have both kinds of imports, eg python
+ perhaps changing jetbrains whinging :-)
o
@elizarov that's true, but it is still enforced in the function not accessing the object's state. One would only need objects when they want to couple state with logic together. Example: Let's say I make a function and I know that it is pure, because I, the author, made sure I am not executing any non-pure code in it. That's great. But now when I put that function inside an object, the caller/user of that function sees
MyObject.myFunction(x)
. People might misread it as not being pure. "If it was a pure function, then why does it need to be inside an object?".
e
This is not an idiomatic train of thought in Kotlin. Object functions in Kotlin’s own libraries are usually pure and live inside object just for the sake of having a common namespace.