Last active
July 10, 2022 18:45
-
-
Save lecuseasar/4a371200c7757f40e6f3b57c37a3b766 to your computer and use it in GitHub Desktop.
Kotlin Extension and Infix Functions
This file contains 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() { | |
println(5.triple()) | |
val list = mutableListOf("emre", "pbu", "arg") | |
list.swap(0, 1) | |
println(list) | |
println(5.topla(1)) | |
println(5 topla 1) | |
println(list[0].birlestir(list[1])) | |
} | |
fun Int.triple() = this * 3 | |
fun MutableList<String>.swap(firstIndex: Int, secondIndex: Int) { | |
val temp = this[firstIndex] | |
this[firstIndex] = this[secondIndex] | |
this[secondIndex] = temp | |
} | |
infix fun Int.topla(value: Int): Int = this + value | |
infix fun String.birlestir(str: String): String = this + str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment