Created
June 12, 2023 11:19
-
-
Save kalaiselvan369/e9c5949586c9ebf91db1719ff61775a8 to your computer and use it in GitHub Desktop.
Extension Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
"Hi".singleQuote() | |
"Hello".strangeQuote() | |
val player = Player(name = "Sachin", team = "India") | |
player.hobbies(player.name) | |
} | |
fun String.singleQuote() = "'${this}'" | |
fun String.doubleQuote() = "\"${this}\"" | |
fun String.strangeQuote(): String { | |
// we can also access all the public member functions of String | |
return this.singleQuote().uppercase() | |
} | |
data class Player(val name: String, val team: String) { | |
private val id: Long = 98 | |
private fun suspend() { | |
} | |
} | |
fun Player.hobbies(name: String): List<String> { | |
//this.id // not possible since it is private | |
//this.suspend() // not possible since it is private | |
return when(name) { | |
"Sachin" -> listOf("Art", "Coins") | |
else -> listOf() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment