← Kotlin Programming learning path
Kotlin Programming · Module 8 of 10

Scope and extension functions

Write expressive code with let, apply, also, run and with.

📖 Key ideas

  1. Extension functions add methods: fun String.isEmail() = contains("@").
  2. let: run code on a non-null value, returns lambda result.
  3. apply: configure an object, returns the object.
  4. also: side effects like logging, returns the object.
  5. run/with: compute a result using the object as receiver.
💡 Real-life example: Scope functions are different grips on the same tool: pick the one that fits the job.
Extension functionAdds a function to a type
letRuns block with it, returns result
applyConfigures this, returns object
alsoSide effect with it, returns object