| Function | Object reference | Return value | Is extension function |
|----------|------------------|----------------|----------------------------------------------|
| let | it | Lambda result | Yes |
| run | this | Lambda result | Yes |
| run | - | Lambda result | No: called without the context object |
| with | this | Lambda result | No: takes the context object as an argument. |
| apply | this | Context object | Yes |
| also | it | Context object | Yes |
| object lambda
-----+----------------
this | apply run
it | also let
scope functions in kotlin
- let
. return (lambda expersion)
. context object (it)
. "generally used to avoid NullPointerException"
@with out lambda expersion
# val name:String? = null
#
# name?.let {
# println(it.reversed())
# println(it.capitalize())
# println(it.length)
# }
@with lambda expersion
# val name:String? = "null"
#
# val stringLength = name?.let {
# println(it.reversed())
# println(it.capitalize())
# it.length
# }
#
# println(stringLength)
- run
. return (lambda expersion)
. context object (this)
. "this is the combination of "(with)"and"(let)"function"
. "this is used to avoid null expection and do operation"
@
# val person:Person? = Person()
#
# val bio = person?.run {
# println(name)
# println(age)
# age+5
# "hy this is the run function"
# }
# println(bio)
#
# class Person{
# var name:String = "hannan"
# var age:Int = 18
# }
"summary"
- with
if you want to operate on a "non-null object"
- let
if you want to just execute lambda expression on a "nullable" object and avoid "nullpointereception"
- run
if you want to operate on a "nullable" object,execute lambda expersion and avoid "NullPointerEception"
- apply
if you wnat ot initialise or configure an object
- also
if you want to do some additional object configuration or operations