DEV Community

Sanjay Prajapat
Sanjay Prajapat

Posted on • Originally published at sanjayprajapat.hashnode.dev

3 2

Type Aliases in Kotlin

type alias provide alternative name for existing type. if type name is longer can be introduces shorter name , it will not introduced new types, they are equivalent to corresponding types.

data class User(
    val name:String? = null,
)

// alias for Int type and ArrayList
    typealias u = Int 
    typealias l = ArrayList<String>
fun main(){
    val users:l = ArrayList()
    users.add("Jb")
    users.add("hdshj")
    print(users)
    val x:u = 34
    print(x)
}
Enter fullscreen mode Exit fullscreen mode
// type alias for inner class 
class A {
inner class Inner
}
class B {
   inner class Inner
}
    typealias AInner = A.Inner
    typealias BInner = B.Inner
Enter fullscreen mode Exit fullscreen mode
 // provide alias for Function type 
 val lamb = { x:Int, y:Int, z:Int -> print("$x $y $z") } 

 typealias MyHandler = (Int, Int, Int) -> Unit
 fun my(f:MyHandler){
     f.invoke(20,21,22)
 }
 fun main(){
     my(lamb)
 }
Enter fullscreen mode Exit fullscreen mode
 typealias Predicate<T> = (T) -> Boolean

 fun foo(p: Predicate<Int>) = p(42)
 fun main(){
     val f1: (Int) -> Boolean = { it > 0 }    
     print(foo(f1)) // true 

     val p: Predicate<Int> = { it > 0 }
     println(listOf(1, -2).filter(p)) // [1]
 }


Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more