If I have an `object` that declares many `const va...
# announcements
m
If I have an
object
that declares many `const val`’s in my Kotlin code, is it possible to star-import all of them into a class that uses them?
e
you can test that in like 30 seconds in your IDE 🙂
m
Yeah, my test tells me “you can’t do that.”
I’m looking for confirmation.
e
Gotcha, I tried and can't do it either. Not sure if there are any neat workarounds 🙂
m
probably not the answer you want to hear, but IMO star imports shouldn’t be used anyway 😄 it’s not hard to import whatever you need on demand, in our team I forbid star imports
💯 2
e
Put them into a package.
👆 1
🙌 1
💡 1
m
I don’t know what that means. The file is in a package…
n
I think Roman means top-level constants in a new subpackage, e.g.
package com.mycompany.those_constants
e
use a package as namespacing instead of object, is the suggestion
m
Ooooo. So take them out of the object, and then you can star-import.
Nice!
Hmmm. Can those constants then be used inside Java code in the same project?
e
yes, if they're defined in a file
foo.kt
they'll be visible to java as static members of a class
FooKt
, just like all top-level declarations
🙌 1
name can be changed with
@file:JvmName
, merged across files with
@file:JvmMultifileClass
, etc.
K 4
🙏 1