data class Order(val id: Int, var status: String, var paidAt: Long?)
fun main() {
val order = Order(42, "pending", null)
// Multi-step mutation/access on the same object
with(order) {
status = "paid"
paidAt = System.currentTimeMillis()
println("Order $id is $status")
}
// with as expression: returns the last value
val summary = with(order) {
"Order $id paid at $paidAt"
}
println(summary)
// Useful for chained API calls on Java objects
val sb = StringBuilder()
with(sb) {
append("Hello, ")
append("world!")
append(" — ")
append(42)
}
println(sb)
}
Create a free account and build your private vault. Share publicly whenever you want.