← Kotlin Programming learning pathKotlin Programming · Module 8 of 10
Scope and extension functions
Write expressive code with let, apply, also, run and with.
📖 Key ideas
- Extension functions add methods: fun String.isEmail() = contains("@").
- let: run code on a non-null value, returns lambda result.
- apply: configure an object, returns the object.
- also: side effects like logging, returns the object.
- 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