KEMBAR78
Kotlin scope functions | PDF
Scope Functions in
Kotlin
{
}
By Waheed Nazir 1
Scope
Scope functions?
Kotlin standard library has extension functions that execute a block of
code on Object and they provide a scope using lambda expression that’s
why they are called Scope Functions.
Types
1) let
2) run
3) with
4) apply
5) also
By Waheed Nazir 2
{
}
Function
scope
Let
run
with
apply
also
e.g
object.let {
//logic
}
let is mostly used for these scenarios
1) To perform actions on non null object using safe call operator ?.
2) Multiple functions calls as chain
3) Helps to create local variable with limited scope in lambda
4) Transformation: Let can return anything from object
By Waheed Nazir 3
{
}print(it.name)
let
!
Output: Waheed
Person object
" it
(You’ll receive calling object as it
inside this scope)
Person data class
it: Person
this (Reference of class)
" this
You can access class instance as
thisAccessing object properties using let
OR: You can
create a local
variable in
lambda scope
# It’s easy
Let on non null objects using
safe call operator ?.
By Waheed Nazir 4
val str: String? = null
str?.let {
// Wouldn’t execute because str object is
null
print(it)
} // No output
Safe Call operator
String variable that can hold
null value
Transformation: Chain of calls using let
Example: Non null check, Transformation, Creating variable
Example: Null check
Transformation:
Creating length
variable, passed
from here
Creating variable from
Transformation
Applying condition
on one property of
object and handling
result using let.
Let examples:
By Waheed Nazir 5
Count all employees
having salary more then
10,000 MYR
Employee Data class
Find total sum of all
employee salaries or Total
salary amount disbursed
List of employees
For loop with index to iterate
filtered list of employees
For each loop, to
iterate employees
You’ll get filter list here in
it
Predicate/logic to filter data
More operations
on employees
list
!
-:Use case:-
operations on
Employees
data e.g. filter,
check salaries,
total amount
paid
"
run is almost same like let, but it is more focused on target object
1) To perform actions on non null object using safe call operator ?.
2) Multiple functions calls as chain
3) Transformation: Run can return anything from object
4) Doesn’t allow to create a local variable in lambda scope
By Waheed Nazir 6
{
}
print(this.name)
OR
Print(name)
run
!
Output: Waheed
Person object
" this
(You’ll receive calling object as this
inside run scope and you can
directly call properties of object or
using this)
Person data class
this: Person
Accessing object’s properties using run
You cannot
create a local
variable in
lambda scope
Run on non null objects using
safe call operator ?.
By Waheed Nazir 7
Safe Call operator
String variable that can hold
null value
Transformation: Chain of calls using run
Example: Non null check, Transformation, Creating variable
Example: Null check
Creating variable from
Transformation
Applying condition
on one property of
object and handling
result using run.
Transformation:
Creating length
variable, passed
from here
Run examples:
By Waheed Nazir 8
Count all employees
having salary more then
10,000 MYR
Employee Data class
Find total sum of all
employee salaries or Total
salary amount disbursed
For loop with index to iterate
filtered list of employees
You’ll get filter list here in
it
More operations
on employees
list
!
-:Use case:-
operations on
Employees
data e.g.
filter, check
salaries, total
amount paid
"
also is mostly used for these scenarios
1) To perform actions on non null object using safe call operator ?.
2) Multiple functions calls as chain
3) Helps to create local variable with limited scope in lambda
4) It returns original object instead of transformation
By Waheed Nazir 9
{
}print(it.name)
let
!
Output: Waheed
Person object
" it
(You’ll receive calling object as it
inside this scope)
Person data class
it: Person
this (Reference of class)
" this
You can access class instance as
thisAccessing object properties using let
OR: You can
create a local
variable in
lambda scope
# It’s easy
Comparison Table:
Function Object reference Return value Is extension
function?
let it Lambda result Yes
also it Context object Yes
run this Lambda result Yes
apply this Context object Yes
with this Lambda result No
By Waheed Nazir 10
Reference, more details
By Waheed Nazir 11https://medium.com/androiddevelopers/kotlin-
standard-functions-cheat-sheet-27f032dd4326
Cheat-sheet:
Jose Alcérreca (Maker & Developer Programs Engineer @ Google – Android)
By Waheed Nazir 12
Cheat-sheet:
Jose Alcérreca (Maker & Developer Programs Engineer @ Google – Android)
https://medium.com/androiddevelopers/kotlin-
standard-functions-cheat-sheet-27f032dd4326
References:
u https://kotlinlang.org/docs/reference/scope-functions.html
u https://medium.com/androiddevelopers/kotlin-standard-functions-cheat-
sheet-27f032dd4326
u https://twitter.com/ppvi/status/1081168598813601793/photo/1
u https://kotlinlang.org/docs/reference/functions.html
u https://www.journaldev.com/19467/kotlin-let-run-also-apply-with
By Waheed Nazir 13

Kotlin scope functions

  • 1.
  • 2.
    Scope functions? Kotlin standardlibrary has extension functions that execute a block of code on Object and they provide a scope using lambda expression that’s why they are called Scope Functions. Types 1) let 2) run 3) with 4) apply 5) also By Waheed Nazir 2 { } Function scope Let run with apply also e.g object.let { //logic }
  • 3.
    let is mostlyused for these scenarios 1) To perform actions on non null object using safe call operator ?. 2) Multiple functions calls as chain 3) Helps to create local variable with limited scope in lambda 4) Transformation: Let can return anything from object By Waheed Nazir 3 { }print(it.name) let ! Output: Waheed Person object " it (You’ll receive calling object as it inside this scope) Person data class it: Person this (Reference of class) " this You can access class instance as thisAccessing object properties using let OR: You can create a local variable in lambda scope # It’s easy
  • 4.
    Let on nonnull objects using safe call operator ?. By Waheed Nazir 4 val str: String? = null str?.let { // Wouldn’t execute because str object is null print(it) } // No output Safe Call operator String variable that can hold null value Transformation: Chain of calls using let Example: Non null check, Transformation, Creating variable Example: Null check Transformation: Creating length variable, passed from here Creating variable from Transformation Applying condition on one property of object and handling result using let.
  • 5.
    Let examples: By WaheedNazir 5 Count all employees having salary more then 10,000 MYR Employee Data class Find total sum of all employee salaries or Total salary amount disbursed List of employees For loop with index to iterate filtered list of employees For each loop, to iterate employees You’ll get filter list here in it Predicate/logic to filter data More operations on employees list ! -:Use case:- operations on Employees data e.g. filter, check salaries, total amount paid "
  • 6.
    run is almostsame like let, but it is more focused on target object 1) To perform actions on non null object using safe call operator ?. 2) Multiple functions calls as chain 3) Transformation: Run can return anything from object 4) Doesn’t allow to create a local variable in lambda scope By Waheed Nazir 6 { } print(this.name) OR Print(name) run ! Output: Waheed Person object " this (You’ll receive calling object as this inside run scope and you can directly call properties of object or using this) Person data class this: Person Accessing object’s properties using run You cannot create a local variable in lambda scope
  • 7.
    Run on nonnull objects using safe call operator ?. By Waheed Nazir 7 Safe Call operator String variable that can hold null value Transformation: Chain of calls using run Example: Non null check, Transformation, Creating variable Example: Null check Creating variable from Transformation Applying condition on one property of object and handling result using run. Transformation: Creating length variable, passed from here
  • 8.
    Run examples: By WaheedNazir 8 Count all employees having salary more then 10,000 MYR Employee Data class Find total sum of all employee salaries or Total salary amount disbursed For loop with index to iterate filtered list of employees You’ll get filter list here in it More operations on employees list ! -:Use case:- operations on Employees data e.g. filter, check salaries, total amount paid "
  • 9.
    also is mostlyused for these scenarios 1) To perform actions on non null object using safe call operator ?. 2) Multiple functions calls as chain 3) Helps to create local variable with limited scope in lambda 4) It returns original object instead of transformation By Waheed Nazir 9 { }print(it.name) let ! Output: Waheed Person object " it (You’ll receive calling object as it inside this scope) Person data class it: Person this (Reference of class) " this You can access class instance as thisAccessing object properties using let OR: You can create a local variable in lambda scope # It’s easy
  • 10.
    Comparison Table: Function Objectreference Return value Is extension function? let it Lambda result Yes also it Context object Yes run this Lambda result Yes apply this Context object Yes with this Lambda result No By Waheed Nazir 10 Reference, more details
  • 11.
    By Waheed Nazir11https://medium.com/androiddevelopers/kotlin- standard-functions-cheat-sheet-27f032dd4326 Cheat-sheet: Jose Alcérreca (Maker & Developer Programs Engineer @ Google – Android)
  • 12.
    By Waheed Nazir12 Cheat-sheet: Jose Alcérreca (Maker & Developer Programs Engineer @ Google – Android) https://medium.com/androiddevelopers/kotlin- standard-functions-cheat-sheet-27f032dd4326
  • 13.
    References: u https://kotlinlang.org/docs/reference/scope-functions.html u https://medium.com/androiddevelopers/kotlin-standard-functions-cheat- sheet-27f032dd4326 uhttps://twitter.com/ppvi/status/1081168598813601793/photo/1 u https://kotlinlang.org/docs/reference/functions.html u https://www.journaldev.com/19467/kotlin-let-run-also-apply-with By Waheed Nazir 13