Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday 11 July 2018

Difference between run, with, let, also and apply in kotlin

Hello dear friends today we are learn about what difference in run, with, let, also and apply . This is master function of the kotlin.

Kotlin  let: 

let take the object is invoked upon as the parameter and return the result of the lambda expression. In other word we also said let is a non-nomadic version of map. It is accept the object as a parameters and returns the result of the lambda. like...

Example 1: 
var student= Student("samset",100)
var result = student.let{ it.rollno=50}

Log.e(" student roll no : "+student)
Log.e("result student roll no : "+result)

OUTPUT:
100
50

here it keyword copy of the property inside the let. let function scope only inside the expression cannot be used outside of the expression.

Example 2: 

 var strlength = str.let { "$it my function".length}
 println("strlength is $strlength") // strlength is 30


class MyTestClass{

   fun testfunction(){
     val str : String = "welcome to kotlin"
     val myresult = str.let{
      
    print(this) // this is instance of MyTestClass 
    print(it)   // it is the  String = "welcome to kotlin" on which we have called let

}
}
}

Chaining let functon:


val strmain="welcome"

// Evolve the value and send to the next chain
 strmain.let{
   println("This is main original string $it") // "welcome"
   it.reversed() // evolve it as parameter to send to next let
}.let{
println("This is reverse string $it") // "emoclew"
it.length   // evolve it as parameter to next let
}.let{
   println("The length of string is $it")  // 7
}

 Outer let:
After nested let used now we are use outer let:

var str="Smaset"
     
      str = str.let{ outer->
        
        outer.let{ inner->
                println(" Inner let is "$inner)
      }
     "Outerlet is $outer"
      }

    println(str)


OUTPUT:

Inner let is samset
Outer let is samset

let check Null:

    var strmain = String? = "welcome"
   strmain ?.let{ println(it)}  // prints welcome
 
    strmain =null
    strmain ?.let{ println(it)} // nothing happens

Kotlin run:

Kotlin run is another interesting function. The following example demonstrates its use cases. run does not return values and does not support it keyword.
var str  =  "Hello welcome to kotlin"
    println(str) //  "Hello welcome to kotlin"
      val str  = "This is a run function" str
}
println(str) // This is a run function

OUTPUT:
  Hello welcome to kotlin
   This is a run function


let with run

      var mytext : String? = null
     
    p?.let{ println(" Text is $mytext ")} ?:
        run { println("Text was null so set default value :"+ mytext ="Default text")}

        println(mytext)


OUTPUT:

Text was null so set default value : Default text 

Kotlin let vs also
also is something same like let but this is return only original value. Rhe also expression return the data class object whereas th let expression return nothing (Unit) as we didn't specify anything explicitly see example for understanding...


   data class Student(var name: String,var rollno : int)
   var stud  = Student("Samset",101)
   
   var s  =  stud.let{ it.name = "sanjoo"}
   var salso  =  stud.also{ it.name = "sanjoo"}

println(s)
println(salso)
println(stud)

OUTPUT:

sanjoo
sanjoo


























No comments:

Post a Comment