# A few Kotlin constructs

DevFeed: [A few Kotlin constructs](<https://devfeed.tech/articles/a-few-kotlin-constructs-25265.md>)

Original publisher: [Read original article](<https://kau.sh/blog/important-kotlin-constructs/>)

Author: Kaushik Gopal

Published: 2022-11-27T12:12:29Z

Content type: tutorial

Language: en

Sources: [Kaushik Gopal's Site](<https://devfeed.tech/sources/kaushik-gopal-s-site.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [interfaces](<https://devfeed.tech/topics/interfaces.md>), [import](<https://devfeed.tech/topics/import.md>), [Java](<https://devfeed.tech/topics/java.md>)

Tags: [import](<https://devfeed.tech/tags/import.md>), [interfaces](<https://devfeed.tech/tags/interfaces.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [type-alias](<https://devfeed.tech/tags/type-alias.md>)

## AI overview

A Kotlin tutorial reviewing several language constructs, including SAM fun interfaces, function types, type aliases, import aliases, and value classes. It explains that function types and interfaces serve similar purposes but are distinct, and notes that import aliases can disambiguate identically named types.

## Source excerpt

A few Kotlin constructs have been introduced into the language over time. I wrote this post as a personal/public service advisory to remind us of their significance. Would love to credit img owner 1. fun interface (SAM) 1.1. (vs) function types 2. type alias 3. import alias 4. value class 5. data object Revisions 1. fun interface (SAM) # Many languages (like Java) did not initially treat functions as first-class citizens. They would emulate this functionality by using an interface with exactly one abstract method (a.k.a Single Abstract Method or SAM) interfaces. fun interface OnClickListener { // single abstract function fun onClick(view: View) } fun Button.setOnClickListenerXXX(listener: OnClickListener) { // listener then invoked at some point listener.onClick(this) } When fun interface is used like the above, we typically must pass an instance of an object that implements this SAM interface. val clickListener = object: OnClick { override fun onClick(view: Button) { Toast.makeText(context, "Button 1 Clicked", Toast.LENGTH_SHORT).show() } } // You can now "pass this function" around myButton.setOnClickListenerXXX(clickListener) In practice, modern JVMs allow a far less verbose version of the above via lambdas: myButton.setOnClickListenerXXX { view -> Toast.makeText(view.context, "testing 1", Toast.LENGTH_SHORT).show() } introduced in Kotlin 1.4 official docs purpose is to achieve first-class functions 1.1. (vs) function types ## Kotlin treats functions as first-class citizens and bakes the concept of "function types" right into the language. Function types and interfaces are not the same thing but serve a similar purpose (treat functions as first-class citizens). The book Effective Kotlin does good justice in pointing out the nuance between these two concepts. At a time when lambdas were not properly supported with most modern JVMs, function types were the elegant alternative. This makes it confusing today (the existence of two similar concepts) because they can ap