Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin var with different return type on getter

Tags:

kotlin

I'd like to hold a property of type X that extends Y, the getter should return Y and setter should be private and accept X.

Is this possible via a simple var definition, or do I need to use fun ?

like image 917
Chen Kinnrot Avatar asked Dec 23 '22 07:12

Chen Kinnrot


1 Answers

Consider using additional backing property:

class Sample {
    private var privateX: X
    var publicY: Y
        get() = privateX
}
like image 75
hluhovskyi Avatar answered Jan 18 '23 15:01

hluhovskyi