I have a data class User
data class User(name: String?, email: String?)
and I created an extension function to get the best identifier (name first, then email)
fun User.getBestIdentifier(): String {
return when {
!this.name.isNullOrBlank() -> this.name
!this.email.isNullOrBlank() -> this.email
else -> ""
}
But I noticed that in my IDE if I get rid of all of the this
words, it still compiles and runs. Like:
fun User.getBestIdentifier(): String {
return when {
!name.isNullOrBlank() -> name
!email.isNullOrBlank() -> email
else -> ""
}
My conclusion was that Kotlin extension functions support member variables implicitly but I am not sure. Does anyone have any documentation of this phenomena or an explanation of why/how it happens?