0Pricing
Kotlin Academy · Lesson

with

Group operations.

The with Function

with is a scope function that takes an object as an argument (not as a receiver of a call) and runs a block with that object as this. It returns the lambda result.

Basic with Usage

You pass the object to with and access its members directly inside the block.

fun main() {
    val name = "Kotlin"
    val description = with(name) {
        "$this has $length characters"
    }
    println(description)
}

All lessons in this course

  1. let and run
  2. apply and also
  3. with
  4. Choosing the Right One
← Back to Kotlin Academy