You cannot use SAM conversion for interfaces with ...
# codereview
g
You cannot use SAM conversion for interfaces with more than one method (Single Abstract Method) Instead you can write extension function where you pass 2 lambdas (also works good with default argument)
a
I see. But what you said was too difficult for me to understand. ­čśé Still newbie right now. On the way to become pro.
g
SAM is a technique from Java 8 (that also used in Kotlin for interop with Java) that allows you to use interface or abstract class with 1 method as lambda
a
I will take a look on that later. Actually I come from PHP. So I do not have any knowledge about OOP. ­čś×
g
Instead, you can do something like this (for this case I just combine 2 callbacks to one, but it works only for cases like this:
Copy code
inline fun TabLayout.setOnTabSelectListener(crossinline listener: (position: Int, reselect: Bool) -> Unit) {
    setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                listener(position. false)
            }

            override fun onTabReselect(position: Int) {
                listener(position. true)
            }
    })
}
Usage:
Copy code
tabLayout.setOnTabSelectListener { position, reselect -> 
   println("selected $position reselect $reselect") 
}
Alternative solution
Copy code
inline fun TabLayout.setOnTabSelectListener(
       crossinline onSelect: ((Int) -> Unit)? = null, 
       crossinline onReselect: ((Int) -> Unit)? = null
) {
setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                onSelect?.invoke(position)
            }

            override fun onTabReselect(position: Int) {
                onReselect?.invoke(position)
            }
    })
}
Usage:
Copy code
tabLayout.setOnTabSelectListener(onSelect = { println("selected") })
a
Thanks @gildor I need some time to disguise this.