Is there a way to rename the default getters and setters in Kotlin? I have a property named in snake_case
, but I still want the getters and setters to be named in camelCase
.
The closest I've gotten is something like
private var property_name = Color.BLACK
private set
fun setPropertyName(c: Color) { property_name = c }
fun getPropertyName() = property_name
Is there a way to do this without hiding the getters and setters and defining new methods?
If you just want to change the name and not the functionality, you can also scope the annotation like so:
@get:JvmName("getPropertyName")
@set:JvmName("setPropertyName")
var property_name = Color.BLACK
It is described in the section on handling signature clashes: https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#handling-signature-clashes-with-jvmname
val x: Int
@JvmName("getX_prop")
get() = 15
So @JvmName("getPropertyName") get
should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With