Kotlin Scope Functions: let, apply, run, with, and also : With Differences
공기문2021. 3. 11. 22:32
class Person{
var name: String = "Hello"
var age: Int = 10
}
fun main(args: Array<String>) {
/** Scope Function : 'with'
* Property 1: Refer to context object by using 'this'
* Property 2: The return value is the 'lambda result'
*/
val person = Person()
println(person.name)
println(person.age)
with(person){
println(name)
println(age)
}
}
class Person{
var name: String = "Hello"
var age: Int = 10
}
fun main(args: Array<String>) {
/** Scope Function : 'with'
* Property 1: Refer to context object by using 'this'
* Property 2: The return value is the 'lambda result'
*/
val person = Person()
val afterFiveYears:Int = with(person){
println(name)
println(age)
age + 5
}
println("$afterFiveYears")
}
class Person{
var name: String = "Hello"
var age: Int = 10
}
fun main(args: Array<String>) {
/** Scope Function : 'with'
* Property 1: Refer to context object by using 'this'
* Property 2: The return value is the 'lambda result'
*/
val person = Person()
val bio: String = with(person){
println(name)
println(age)
age + 5
"He is a freak"
}
println(bio)
}
class Person{
var name: String = ""
var age: Int = 0
}
fun main(args: Array<String>) {
/** Scope Function : 'apply'
* Property 1: Refer to context object by using 'this'
* Property 2: The return value is the 'context result'
*/
val person = Person().apply {
name = "Sriyank Siddartha"
age = 10
}
}
class Person{
var name: String = ""
var age: Int = 0
}
fun main(args: Array<String>) {
/** Scope Function : 'apply'
* Property 1: Refer to context object by using 'this'
* Property 2: The return value is the 'context result'
*/
val person = Person()
person.apply {
name = "Sriyank Siddartha"
age = 10
}
}
class Person{
var name: String = ""
var age: Int = 0
}
fun main(args: Array<String>) {
/** Scope Function : 'apply'
* Property 1: Refer to context object by using 'this'
* Property 2: The return value is the 'context result'
*/
val person = Person()
person.apply {
name = "Sriyank Siddartha"
age = 10
}
with(person){
println(name)
println(age)
}
}
fun main(args: Array<String>) {
/** Scope Function : 'also'
* Property 1: Refer to context object by using 'it'
* Property 2: The return value is the 'context object'
*/
val numbersList: MutableList<Int> = mutableListOf(1, 2, 3)
// Some other code... may be a function call or a program to swap two numbers
// Operations on the 'numbersList'
val duplicateNumbers = numbersList.also {
println("The list elements are: $it")
it.add(4)
println("Ths list elements after adding an elements : $it")
it.remove(2)
println("Ths List elements after removing an element: $it")
}
println("The original number $numbersList")
println("The duplicate number $duplicateNumbers")
}
class Person{
var name: String = ""
var age: Int = 0
}
fun main(args: Array<String>) {
val person = Person().apply {
name = "Syrinks"
age = 20
}
with(person){
println(name)
println(age)
}
// Perform some other operations on 'person' object
person.also {
it.name = "Syrinks hello"
println("${it.name}") // prints New name : Shreks form smartherd
}
}
class Person{
var name: String = ""
var age: Int = 0
}
fun main(args: Array<String>) {
/** Scope Function : 'let
* property 1 : refer to context object by using 'it'
* property 2 : The return value is the 'lambda result'
*/
// Use 'let' function to avoid NullPointerException
val name: String? = null
name?.let {
println(it.reversed())
println(it.capitalize())
print(it.length)
}
}
fun main(args: Array<String>) {
/** Scope Function : 'let
* property 1 : refer to context object by using 'it'
* property 2 : The return value is the 'lambda result'
*/
// Use 'let' function to avoid NullPointerException
val name: String? = "Hello"
val stringLength = name?.let {
println(it.reversed())
println(it.capitalize())
it.length
}
println(stringLength)
}
class Person{
var name: String = ""
var age: Int = 0
}
fun main(args: Array<String>) {
/** Scope Function : 'run'
* property 1 : refer to context object by using 'this'
* property 2 : The return value is the 'lambda result'
*
* 'run' is combination of 'with' and 'let'
* If you want to operate on a Nullable object and avoid NullPointerException then use 'run'
*/
val person: Person? = null
val bio = person?.run {
println(name)
println(age)
age + 5
"He is a freak"
}
println(bio)
}