Scope Functions: let , run, with, apply, & also
package com.example.my_kotlin_practice
fun main(args: Array<String>) {
// Scope functions
// -let
// -run
// -with
// -apply
// -also
var emp = Employee()
// emp.id = 1
// emp.name = "ABC"
var x = emp.apply {
this.id = 1
this.name = "ABC"
}
println("Apply : ${emp.id}")
var emp1:Employee? = Employee()
var y = emp1?.let {
println("Let : ${it.id}")
println("Let : ${it.name}")
// 4
}
var z = with(emp) {
println("With : ${this.name}")
}
var p = emp.run {
println("Run : ${this.name}")
}
var c = emp.also {
println("Also : ${it.name}")
true
}
println(c.name)
}
class Employee{
var id:Int = 0
var name:String = ""
}
OUTPUT :
Apply : 1 Let : 0 Let : With : ABC Run : ABC Also : ABC ABC Process finished with exit code 0
Comments
Post a Comment