Skip to content

Instantly share code, notes, and snippets.

@kalaiselvan369
Created June 12, 2023 11:19
Show Gist options
  • Save kalaiselvan369/e9c5949586c9ebf91db1719ff61775a8 to your computer and use it in GitHub Desktop.
Save kalaiselvan369/e9c5949586c9ebf91db1719ff61775a8 to your computer and use it in GitHub Desktop.
Extension Function
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