What’s the right way to do a Data Class such that ...
# announcements
m
What’s the right way to do a Data Class such that it can “implement an interface that requires one getter”? The data class is just doing what it’s doing, I just need to pass it to a method that requires one getter (which it would have)
s
java interface?
m
yeah.
s
if you need to reuse this class, you can always just make the field a
private/internal val
and implement an
override fun getFoo()
m
It’s just a pojo - a bag of DB columns.
s
I’m not sure what you’re implying by that
the getter you implement is just a utility getter to satisfy the interface’s requirements
unfortunately we can’t use kotlin property getters and setters to do so - we just run into JVM platform declaration clashes
m
Just trying to make sure I’m understand the limitations and solutions. I just need to make that particular property private and expose getter/setter methods, like I would do in Java?
s
More or less, yes. If it’s a
val
you of course don’t have to write a setter
also if you make it an
internal val
, you can avoid having to use the getter within Kotlin
so in your Kotlin code you can still just call
pojo.field
this is specifically for Java interfaces. unfortunately JB hasn’t really given us an elegant solution to this issue
it might honestly be by design, in order to differentiate between interface methods and properties in general
m
It’s a frustrating ambiguity in the java world.
Hmm. If I make the interface kotlin, and shift the work to the java side, that actually seems smoother.
Since in a kotlin interface, I can have a property.
🎉 1
Thanks for going through that with me. 🙂
s
no problem! everyone needs a rubber duck from time to time lol