The extenstion function is designed for situations where you want to add a function to a built-in or third-party class. You cannot do this by default because built-in functions are not modifiable.
An example implementation to add a toUnsigned method to the built-in Byte class:
fun Byte.toUnsigned(): Int {
return if (this < 0) this + 256 else this.toInt()
}
As Byte is a built-in class you cannot modify it directly. However, you can define an extension function as per above code. You can then call the extension function in the following way:
val x: Byte = -1
println(x.toUnsigned()) // Prints 255
Keep in mind that this is just syntactic sugar - you're not actually modifying the class or its instances. Therefore, you have to import an extension function/property wherever you want to use it (since it isn't carried along with the instances of the class).
Source : https://kotlinlang.org/docs/tutorials/kotlin-for-py/extension-functionsproperties.html