Updated April 6, 2023
Introduction to Kotlin typealias
In kotlin typealias is one of the feature and the concept to change the existing name and it provides the new name so it comes from the alternative way for the existing type to reflect the changes. its moreover to read the code it mainly support with the string type of datas that data may be of too long and we can declare the shorter name for the new one instead of the existing. it does not support the new type and it does not introduce the new types compiler will translates the data from one type to another type of typealias Predicate<T>.
Syntax
The kotlin language supports many different features like classes, methods, and keywords to implement the application. Like that typealias is one of the concept and feature for to create the new name instead of the existing name from the kotlin codes.
typealias name = Pair<datatype1, dataype2> //Pair is one of the data class type and it is used to store the two values as the paarmeter
typealias name1<T>=(T) -> datatype3
fun main()
{
--some logic codes depends on the requirement---
}
The above code is the basic syntax for utilizing the typealias in the kotlin code. We can use any where of the programming syntax like classes, methods, and other defined variables.
How do typealias work in Kotlin?
The typealias is the concept and keyword for to change the existing name and it will be replaced with new name of any types that going to be considered as the datatype. When we declare or write the code using typeAlias it should be declared from the top level of the kotlin code and not like the inside of the class or function. If we want to declare it from inside of the function and class it will always throw an error and it. While using this typeAlias names on the code the developer will easily understand the codes and the workflow should be aligned and performed it. We can define the typealias with many areas like collections and other packages. We also define the typealias with any type of kotlin files but it must be outside of the class we cannot define the typealias at inside of any classes as nested and also local type aliases also not supported for these areas. The inner classes of the kotlin uses the type aliases that will be either long or short names but it should be the meaningful one. If we can also use the typealias with two classes like parent-child or the classes should be on the same name but it must be the different packages. In that time tyepAlias is referenced by the second class by the fully qualified class names.
Examples of Kotlin typealias
Below are the different examples of Kotlin typealias:
Example #1
Code:
typealias tg1 = Pair <String, String>
typealias Number<T> = (T) -> Boolean
fun main() {
val inp = tg1("Pampers is the registered trademark of the Procter and gamble company", "It has many different areas like Chennai, Mumbai")
val inp2 = tg1("Andheri is one of the area for trade name and manufacturing", "address road and it read the last alphabet characters on first line of side in the coding panel")
val inp3=tg1("Build ID: C8_7_2_14NOV2020_982289c645,Do Server installation, Do apps installation and don't select Process Management", "Proceed with installation After installation completes, navigate to Setup - System Configuration Expected: Process Management tab should not be present Actual: Process Management tab is present")
println(inp)
println(inp2)
println(inp3)
val y: Number<Int> = { it > 0 }
println("Welcome To My Domain its the first example that related to the Kotlin typealias concept: "
+listOf(13, -23, 33, -43, 53 , 63, -73,83,-93).filter(y))
}
Output:
In above example, we used typealias basic example and it will be used in both string and integer data type.
Example #2
Code:
typealias tg2 = Pair <String, String>
fun main(args: Array<String>){
var lst: List<String> = listOf<String>("Welcome", "To My ","Domain", "Second example realted","to Kotlin typealias")
var lst1: List<String> = listOf<String>("Have", "a nice", "day users")
for(itrt in lst){
print(itrt+" ")
}
println()
println(lst.get(0))
println(lst.indexOf("Vijay"))
println(lst.lastIndexOf("Vijay"))
println(lst.size)
println(lst.contains("Prakash"))
println(lst.containsAll(lst1))
println(lst.subList(2,4))
println(lst.isEmpty())
println(lst.drop(1))
println(lst.dropLast(2))
val sec=tg2("Key is the Months first three letters", "Values are extracting the months")
var str1: String? = null
var str2: String? = "Welcome To My DOmain is the first example that relates to the Kotlin Switch statement"
var strlen1: Int = if (str1 != null) str1.length else -1
var strlen2: Int = if (str2 != null) str2.length else -1
println("Length of str1 is ${strlen1}")
println("Length of str2 is ${strlen2}")
val vars = readLine()
val vars1 = when (vars) {
"jan" -> "January"
"feb" -> "February"
"mar" -> "March"
"apr" -> "April"
"may" -> "May"
"jun" -> "June"
"jul" -> "July"
"aug" -> "August"
"sep" -> "September"
"oct" -> "October"
"nov" -> "November"
"dec" -> "December"
else -> "Please enter your inputs?"
}
println("$vars1")
println("Thank you users for spending the time with our application")
}
Output:
In second example we used collection concepts additionally by using the typealias feature for to implement the application.
Example #3
Code:
typealias tg3 = Pair <String, String>
fun eg(exam: (id: String)->Unit, id: String){
println("Welcome To My Domain its the third example regarding kotlin typealias")
println("Have a Nice Day users")
exam(id)
}
inline fun eg1(exam: (id: String)->Unit, id: String){
}
class Test {
var addr:String="Flat 12C, Arvind Enclave, Sathyamoorthy street, Srinivasa nagar, chengalpattu district, chennai-600009"
fun stddets(){
println("We have entered the details like Address etc. the information’s are encrypted and it is stored in the separate database in cloud applications")
}
companion object eg{
var empAddress:String="Flat 12C, Sathya Enclave, Sathyamoorthy Nagar, Muthamizh Theru, chengalpattu district, chennai-600010"
fun details(){
println("Your details are welcome")
}
fun empdetails(){
println("${this.empAddress}")
println("We have entered the details like employee Address etc the informations are encrypted and it is stored in the separate database in cloud applications")
}
}
}
fun main() {
val sec=tg3("Planet","Earth")
eg({ id: String ->
println("Thank you users for spending the time")
println("Kindly keep trying and spent your valuable time with us $id")
}, "Welcome Users")
Test.details()
Test.eg.details()
Test.eg.empdetails()
println("Thank you users for spending the time with the companion object application")
}
Output:
The final example we used class and companion object with the typealias concept.
Conclusion
In kotlin typealias is the feature for to change the existing names and it creates the new name. It will be called wherever it requires from the code. Main thing is it will declare the outside areas of the class, functions, etc. It should be declared initially on the kotlin file.
Recommended Articles
This is a guide to Kotlin typealias. Here we discuss the introduction, syntax, and working of typealias in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –