Kotlin Extension Property

This blog was originally posted at https://agrawalsuneet.github.io/blogs/kotlin-extension-property/ and reposted on Medium on 13th Sept 2020.

Is it even possible?

Can we add a property/variable as an extension to an existing class?

According to the theory of extension functions, this is not possible since you can only add the extension functions but not properties, but there is a workaround which can solve the purpose.

class Temperature(var tempInCelsius: Float)
var Temperature.tempInFahrenheit: Float
get() = (tempInCelsius * 9 / 5) + 32
set(value) {
tempInCelsius = (value - 32) * 5 / 9
}

Please continue reading at https://agrawalsuneet.github.io/blogs/kotlin-extension-property/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store