Updated April 4, 2023
Ā
Ā
Introduction to Kotlin also
Kotlin also is one of the keyword functions that can be used to define the block and it does not access its receiver parameter for all not mutated to its parameter receiver if the coder has planned to does not return any different values also() is similar to the let functionality in terms of referring to the context of the object as āitā and not āthisā also in providing null safety checks while doing a chain of operations helps in evaluating the current operating values if required the also() always return the same object which it is referring to the same class.
Syntax
The kotlin has many default keywords, classes, methods, and other utility functions for implementing mobile-based applications. Like that kotlin also() is one of the keywords and its scope function for style programming is highly advocated and supported by kotlinās syntax.
data class name(parameters)
var name1=name(values)
var vars=name.also{
----some logic codesā
}
fun main()
{
--some main programming and create instance for calling the methods---
}
The above code is the basic syntax for utilizing the also() function in the kotlin language. We can utilize the also() function in various areas it depends on the requirement.
How Also works in Kotlin?
The kotlin classes, functions used for various scenarios in the application sometimes we used both default and customized functions and keywords in the language. Although these functions will do very similar things and there are more important differences in their method signatures and their implementation. These differences dictate how they must be used and let’s compare the other function like with() function to the signature and implementation of one of the other functions the also() function is defined with the other similar functions.
It has some little difference when compared to with() and other types of functions. Generally, the receiver argument is provided as an explicit parameter like T in the case of with() function whereas incase of also() its provided as an implicit receiver T. Mainly the block type of argument is defined as the function that has an implicit receiver T in the case of with() function whereas it has an explicit argument type T in the case of also() function. The with() function generally it returns what is returned by executing its block argument whereas the also() function is returned the same type of object that was provided as its receiver type. Using the also() if the block does not access itās the receiver parameter at all or if does not mutate itās the receiver parameter so the also() block does not need to return a different value.
Examples of Kotlin also
Here are the following examples mentioned below:
Example #1
Code:
data class PC(val pcName: String)
class pcBrand<T>(val lst: List<T>) {
fun pcBrand(): List<T> {
return lst.filterIndexed { id, _ -> id % 4 == 2 }
}
}
fun <T> ArrayList<T>.filterOnCondition(condition: (T) -> Boolean): ArrayList<T>{
val rest = arrayListOf<T>()
for (itm in this){
if (condition(itm)){
rest.add(itm)
}
}
return rest
}
fun demo(str1: String,str2: String, examp: (String,String) -> String): Unit {
val out = examp(str1,str2)
println(out)
}
fun main() {
println("Welcome To My Domain its the first example that related to the kotlin also() function")
var x = 1
x = x.also { it + 2 }.also { it + 4 }
println(x)
val lststr = listOf("Samsung", "Sony", "Lenovo")
val rslt: pcBrand<String> = pcBrand(lststr)
println(rslt.pcBrand())
val lstInt = listOf(5, 10, 15, 20, 25, 30)
val rstInt = pcBrand(lstInt)
println(rstInt.pcBrand())
val tvlst = listOf(
PC("Samsung"),
PC("Sony"),
PC("Lenovo"),
PC("Wipro"),
PC("HCL")
)
var out = pcBrand(tvlst).pcBrand()
println(out)
println("Have in Nice Day users please try again")
val examp:(String,String)->String={st1,st2->"$st1 assigned and it will be automatically goes to $st2"}
demo("First Input","Second Input",examp)
var month = arrayListOf<String>()
month.add("January is the first month")
month.add("February is the second month`")
month.add("March is the third month")
month.add("April is the fourth month")
month.add("May is the fifth month")
month.add("June is the sixth month")
month.add("July is the seventh month")
month.add("August is the eight month")
month.add("September is the ninth month")
month.add("October is the tenth month")
month.add("November is the eleventh month")
month.add("December is the twelth month")
var y = month.filterOnCondition { it.contains("August") }
val z = { print("Have a Nice day users please continue to spending your time with us") }
demo1(z)
}
fun demo1(z: () -> Unit) {
z.invoke()
}
fun demo2(p: Int, q: Int,r: (Int) -> Unit){
val result = p + q
println(result)
}
Output:
The above example we used also() with the collection and other util methods for achieving the user task.
Example #2
Code:
fun main() {
println("Welcome To My Domain its the second example that related to the kotlin also() function")
var p = 1
p = p.also { it + 3 }.also { it + 6 }
println(p)
data class Second(var strvalue: String, var intvalue: Int)
val operation = Second("Sivaraman", 32)
val outp1 = operation.also { operation -> operation.intvalue = 34 }
println(operation)
println(outp1)
val operation1 = Second("Sivaraman", 32)
val outp2 = operation.let { it.intvalue * 16 }
println(operation1)
println(outp2)
val operation2 = Second("Sivaraman", 32)
val outp3 = operation.apply { intvalue = 34 }
println(operation2)
println(outp3)
val operation3 = Second("Sivaraman", 32)
val outp4 = operation.run { intvalue * 8 }
println(operation3)
println(outp4)
}
Output:
In the second example we used also(), apply(), let() are the different keywords and print each operation on the console.
Example #3
Code:
fun main() {
println("Welcome To My Domain its the third example that related to the kotlin also() function")
var inp1 = "Sivaraman"
inp1 = inp1.also { it + "Chennai" }.also { it + "Tiruppur" }
println(inp1)
data class Third(var strvalue: String, var strvalue2: String)
val third = Third("Arun", "kuwait")
val res = third.also { third -> third.strvalue2 = "Chennai" }
println(third)
println(res)
}
Output:Ā
The final example we used also() keyword append with the String data type and to return the results on the console screen.
Conclusion
In kotlin language has many different features, variables, and other keywords for achieving the mobile-based application with user scenarios and its requirements. Among that the function side also() is one of the scoping types of function and passes the object and other types of values as the argument and returns the same object as the value.
Recommended Articles
This is a guide to Kotlin also. Here we discuss the introduction, syntax, and working of also in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more