Can anyone suggest the best way to get the current...
# announcements
p
Can anyone suggest the best way to get the current data class’ package name in the constructor as a default value?
Does packageName var do what I’m looking for?
Copy code
data class Clickable(val id: String, val label: String,
                             val className: String = MainActivity::javaClass.name) {
    lateinit var packageName: String
    init {
        if (packageName.isEmpty()) {
            this.javaClass.`package`?.let {
                packageName = it.name
            }
        }
    }
}
Seems not as you can’t specify the packageName
d
Have you tried referencing the java class through
::class.java
instead of
::javaClass
? Not really sure why, but the latter does not compile here
p
Yeah, I think that cleans some things up:
Copy code
data class Clickable(val id: String, val label: String,
                             val jClass: Class<out Activity> = MainActivity::class.java) {
    val className: String = jClass.name
    val packageName: String = jClass.`package`!!.name
}
👍 1