The type of function is as follows:
(Parameters) -> ReturnType
In your case, instead of using an interface type, you can use (View, Int) -> Unit . It will look something like this:
private var onSomeActionListener: ((View, Int) -> Unit)? = null fun setOnSomeActionListener(listener: (View, Int) -> Unit) { onSomeActionListener = listener } private fun callSomeActionListener(view: View, position: Int) { onSomeActionListener?.invoke(view, position) }
Add names
In function types, you can also specify parameter names. This doesn't change much under the hood, but they can add some clarity here and in the calling code, which is nice.
(view: View, position: Int) -> Unit
Using type alias
To avoid having to enter (View, Int) -> Unit each time, you can define typealias:
typealias OnSomeActionListener = (view: View, position: Int) -> Unit
So now your code looks like this:
private var onSomeActionListener: OnSomeActionListener? = null fun setOnSomeActionListener(listener: OnSomeActionListener?) { onSomeActionListener = listener }
And to call him:
val thing = SomeClass() thing.setOnSomeActionListener { view, position ->
source share