Skip to content

Instantly share code, notes, and snippets.

@Kolyall
Last active November 13, 2019 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kolyall/d20240639ff9af38b763e01485742d5e to your computer and use it in GitHub Desktop.
Save Kolyall/d20240639ff9af38b763e01485742d5e to your computer and use it in GitHub Desktop.
Kotlin Functions
for ((index, value) in array.withIndex()) {
println("the element at $index is $value")
}
for (index in array.indices){
println("the element at $index is ${array[index]}")
}
for (index in 0 until array.size){
println("the element at $index is ${array[index]}")
}
class MyClass {
fun test() {
val str: String = "..."
val result = str.xxx {
print(this) // Receiver
print(it) // Argument
42 // Block return value
}
}
}
╔══════════╦═════════════════╦═══════════════╦═══════════════╗
║ Function ║ Receiver (this) ║ Argument (it) ║ Result ║
╠══════════╬═════════════════╬═══════════════╬═══════════════╣
║ let ║ this@MyClass ║ String("...") ║ Int(42) ║
║ run ║ String("...") ║ N\A ║ Int(42) ║
║ run* ║ this@MyClass ║ N\A ║ Int(42) ║
║ with* ║ String("...") ║ N\A ║ Int(42) ║
║ apply ║ String("...") ║ N\A ║ String("...") ║
║ also ║ this@MyClass ║ String("...") ║ String("...") ║
╚══════════╩═════════════════╩═══════════════╩═══════════════╝
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment