Scope Functions
With Receiver
Use with to call several members on the same receiver.
With Receiver
WithReceiver.kt
class ScoreCard(val base: Int) {
fun total(bonus: Int): Int {
return base + bonus
}
}
fun main() {
val bonus =
val card = ScoreCard(10)
val total = with(card) {
total(bonus)
}
println("bonus=$bonus")
println("total=$total")
}
class ScoreCard(val base: Int) {
fun total(bonus: Int): Int {
return base + bonus
}
}
fun main() {
val bonus =
val card = ScoreCard(10)
val total = with(card) {
total(bonus)
}
println("bonus=$bonus")
println("total=$total")
}
class ScoreCard(val base: Int) {
fun total(bonus: Int): Int {
return base + bonus
}
}
fun main() {
val bonus =
val card = ScoreCard(10)
val total = with(card) {
total(bonus)
}
println("bonus=$bonus")
println("total=$total")
}
with
`with` takes a receiver object and returns the block result.