Skip to content

Instantly share code, notes, and snippets.

@shrawan2015
Created January 25, 2019 11:09
Show Gist options
  • Save shrawan2015/bd14656ae32bcfee52d0efa4881764e5 to your computer and use it in GitHub Desktop.
Save shrawan2015/bd14656ae32bcfee52d0efa4881764e5 to your computer and use it in GitHub Desktop.
difference in let,run,with,apply,also in kotlin
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("...") ║
╚══════════╩═════════════════╩═══════════════╩═══════════════╝
Ref:https://proandroiddev.com/the-difference-between-kotlins-functions-let-apply-with-run-and-else-ca51a4c696b8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment