Updated April 5, 2023
Introduction to Kotlin let
The kotlin let is one of the default keywords and it is a function that is used to apply some user-defined operations on any of the objects, classes, and methods finally it returns the function because the let is one of the scope functions so it will return the lambda expression the return type also been void and it is the extension function to the template class so that it takes the lambda parameter and apply it on the contract ultimately it returns the execution of the lambda which we passed as the parameter for to performing the operations.
Syntax
In kotlin language, we used a lot of default keywords, variables, and functions. Among that scope function is one of the primary functions and features for performing the extension function, nullability check, and much more.
class name{
var eg;
}
val n:name?=name() //object creation
n?.let{
-----some logic codes—
}
The above code is the basic syntax for utilizing the let keyword and function in the kotlin language. We can use let function in different areas based on the requirement.
How does let work in Kotlin?
In the kotlin language, we used a lot of default keywords and functions used for implementing the kotlin mobile-based application. Like kotlin scope is one of the function types and it is used to for to providing the features like extension functions, nullability checks, and much more. Scope functions are nothing but the functions which define the scope of the calling object we can apply the operations on that object within that scope. And also it will return the object itself from that the scope function or we can even return the result of the operation or operations from the scope function. So that let is the extension function for to create the template class which takes the lambda as the parameter, for applying the contract on it and it ultimately returns the execution of the lambda values which we passed as the parameters on it. The let function has the return type but the last expression which we returned from our passed lambda parameter and is the extension function for the template class and it can be called as an object. Basically, the kotlin let is often used for executing the code block only with non-null values and to perform the actions on a non-null object by using the call safe operator. Based on that let is act as a separate function and not an extension in kotlin so that the calling method is different from the others.
Examples
Given below are the examples mentioned:
Example #1
Code:
class Example1
{
fun first(){
var lst=ArrayList<String>()
lst.add("Tamil is one of the language subject that relates to the history and known facts of the Tamil peoples")
lst.add("English is another language subject that also the international language which knows world facts")
lst.add("Maths is one of the subject that relates to the mathematical concepts")
lst.add("Computer science is one of the subject that relates to the computer language")
lst.add("Physics is the subject that relates to the scientists and concepts relates to the research")
lst.add("Chemistry is the subject that related to the chemical concepts and details")
lst.add("Botany is the subject that describes about the natural plant like trees and leaves etc.")
lst.add("Zoology is the subject it describes about the humans diseases and parts")
lst.add("History is the subject that details about the historical concepts")
lst.add("Geography is the subject that details about the earth and other planets")
for(x in lst)
print("The arryList are iterated please find your input values $x ")
}
}
fun main(args: Array<String>) {
var inp1 = "Welcome To My Domain its the first example related to the kotlin let function"
inp1.let { println("$it!!") }
println(inp1)
val eg = listOf("January is the first month", "February is the second month", "March is the third month", "April is the fourth month","May is the fifth month","June is the sixth month",
"July is the seventh month", "August is the eight month", "September is the ninth month","October is the tenth month","November is the eleventh month","December is the tweleth month")
println(eg.groupBy { it.first().toUpperCase() })
println(eg.groupBy(keySelector = { it.first() },
valueTransform = { it.toUpperCase() }))
val lst1 = 1.rangeTo(19).toList()
println(lst1.groupBy { it%7 })
val out=Example1()
println(out.first())
}
Sample Output:
In the first example, we used let function with collection concepts like ArrayList and its functionality is added to the above code.
Example #2
Code:
class Company {
var cmpName: String? = null
var cmpYear: Int = 0
fun companydetails(){
var inp2=listOf("Sathyamoorthy street", "moorthy nagar", "chengalpattu district", "chennai")
println(inp2.groupBy { it.first().toUpperCase() })
println(inp2.groupBy(keySelector = { it.first() },
valueTransform = { it.toUpperCase() }))
println("Welcome To My Domain this is the second example regarding the kotlin let function")
println("We have entered the customer details like customer id, customer name,customer salary, customer Address etc. the information’s are encrypted and it is stored in the separate database in cloud applications")
}
}
fun main()
{
println("Welcome To My Domain its the second example related to the kotlin let functiom")
val Company: Company? = Company()
Company?.cmpName = "VSR Organizations"
Company?.cmpYear = 1989
Company?.let {
println(it.cmpYear)
}
Company?.companydetails()
}
Sample Output:
In the second example, we used let method to declare the class-like company and to operate their operations.
Example #3
Code:
fun main()
{
println("Welcome To My Domain its the third example related to the kotlin let functiom")
data class Shoes(var shoename: String, var shoebranch : String)
var shoe = Shoes("Adidas, Bata, Asics, VSR, Lancer, puma, Van heusen, Axter, K' Footlance", "Chennai")
var var1 = shoe.let { it.shoebranch = "Chennai" }
var var2 = shoe.also { it.shoebranch = "Chennai" }
println(var1)
println(var2)
println(shoe)
}
Sample Output:
In the final example, we used shoe details like shoe names, shoe branch, etc. We used both let and also method to call the shoe branch in the loop.
Conclusion
In kotlin language lot of default keywords, functions, and classes to operate and implement the application. Like that let is one of the scope functions and to perform their behaviors in the kotlin application which depends on the requirement. It acts as the extension to be used on the kotlin templates.
Recommended Articles
This is a guide to Kotlin let. Here we discuss the introduction, syntax, and working along with different examples and code implementation. You may also have a look at the following articles to learn more –