Spreadsheet for differences between let, with, run, apply and also

The power of with is in fact really understated. It de-facto allows context oriented programming. For example you can add some additional features to existing objects when running them inside specific context. Like this:

object ContextA{
  val <T> Map<String, T>.something
    get() = this["A"]
}

object ContextB{
  val <T> Map<String, T>.something
    get() = this["B"]
}

val map Map<String, String> = ...

with(ContextA){
  doSomething(map.something)
}

You can even pass the context as a parameter. I find it really fascinating.

2 Likes