https://kotlinlang.org logo
Title
w

wbertan

03/29/2019, 10:01 AM
Trying to understand few things... Was trying to create an extension:
fun View.setOnClickListenerWithThrottle(@Nullable listener: View.OnClickListener?) {
        // body
    }
Was planning to use like the Android `setOnClickListener`:
// This is the Android 'public void setOnClickListener(@Nullable OnClickListener l)'
        view.setOnClickListener {
            //onClick action
        }
But I'm getting this error:
/*
            Type mismatch.
            Required: View.OnClickListener?
            Found () -> Unit
        */
        view.setOnClickListenerWithThrottle {
            //onClick action
        }
Any idea or suggestion? Someone knows why or how I could achieve the desired behaviour?
g

gildor

03/29/2019, 10:03 AM
Because there is no SAM coversion for Kotlin functions (at least now)
Use this instead:
fun View.setOnClickListenerWithThrottle(listener: ((View) -> Unit)?) {

    }
w

wbertan

03/29/2019, 10:07 AM
Wow, nice! thank you! it works perfect! Already voted in the link 🙂
a

ahulyk

03/29/2019, 10:09 AM
g

gildor

03/29/2019, 10:10 AM
Yes, experimental support already there, but I wouldn’t enable new inference only for this case
👍 1
Also, this will be disabled by default in new type inference soon https://youtrack.jetbrains.com/issue/KT-30661
a

ahulyk

03/29/2019, 10:25 AM
@gildor thanks - good to know that!
j

Jacques Smuts

03/29/2019, 10:40 AM
Ran into exactly the same issue when I Extended some RxJava functions. Glad to know this type inference will be easier in the future 🙂
a

ahulyk

03/29/2019, 11:05 AM
@Jacques Smuts with RxJava you can use RxKoltin (it has same helpers for that) or enable new type inference