Type Aliases in Kotlin

This post was originally posted at https://agrawalsuneet.github.io/blogs/type-aliases-in-kotlin/ and reposted on Medium on 23rd Dec 2018.
typealias DemoCustomView =
val demoView = DemoCustomView(context)
val demoView = 
typealias MapIntToList = HashMap<Int, List<String>>
val map = MapIntToList()
typealias MapIntToTemplate<T> = HashMap<Int, T>//and then to use itval stringMap = MapIntToTemplate<String>()
val mapOfLists = MapIntToTemplate<List<Int>>()
class DemoClass {    interface ViewHolderCallback

inner class CustomViewHolder
typealias ViewHolderCallbackInner = com.agrawalsuneet.demoapp.common.DemoClass.ViewHolderCallback

typealias CustomViewHolderInner = com.agrawalsuneet.demoapp.common.DemoClass.CustomViewHolder

//another example
typealias AndroidColors = android.R.color
typealias ProjectColors = R.color
ContextCompat.getColor(this, ProjectColors.colorPrimary)
ContextCompat.getColor(this, AndroidColors.black)
typealias Conditional<T> = (T) -> Booleanval oddFilter: Conditional<Int> = { it % 2 == 1 }print(listOf(1, 2, 3, 4).filter(oddFilter))//orval fourDigitFilter : Conditional<String> = { it.length == 4}print(listOf("abc", "abcd", "abcde").filter(fourDigitFilter))





Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal


More from Medium

Kotlin Collections & Operations

How to Use TriState Toggle in Jetpack Compose

3 Best Practices for Using Named Arguments in Kotlin

Kotlin Flows Unit Tests

A screen shot of a popup window that helps you generate a test class