Properties that act as aliases for other properties generate unnecessary getters and setters


#1

Say I have an example data class called Vec2f that looks like this:

data class Vec2f(var x: Float, var y: Float) {
    inline var r: Float get() = x; set(value) { x = value }
    inline var g: Float get() = y; set(value) { y = value }
}

In the decompiled bytecode for this class it creates methods called getR(), getS() and the relevant setters, which just call the getX() and Y respectively, as well for the setters. However whenever I use the properties r and g in normal code (e.g. vec.r) instead of showing as getR() it will call getX(), this is to do with how it inlines the property I assume, however this means there are redundant methods defined in the Vec2f that coverage tests will always mark as missed, as well as it adds unnecessary bytecode and methods. I couldn’t find much about this so if this is by design then fair enough but I thought it was interesting enough to inquire.


#2

I might be wrong, but I think this has to do with java interop. If you use r from within kotlin the compiler knows to inline it and everything will be fine, but I think there will still be a getter and a setter generated so you can call them from Java. I can’t find any official reference to this though.