Trying to understand few things... Was trying to c...
# android
w
Trying to understand few things... Was trying to create an extension:
Copy code
fun View.setOnClickListenerWithThrottle(@Nullable listener: View.OnClickListener?) {
        // body
    }
Was planning to use like the Android `setOnClickListener`:
Copy code
// This is the Android 'public void setOnClickListener(@Nullable OnClickListener l)'
        view.setOnClickListener {
            //onClick action
        }
But I'm getting this error:
Copy code
/*
            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
Because there is no SAM coversion for Kotlin functions (at least now)
Use this instead:
Copy code
fun View.setOnClickListenerWithThrottle(listener: ((View) -> Unit)?) {

    }
w
Wow, nice! thank you! it works perfect! Already voted in the link 🙂
a
g
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
@gildor thanks - good to know that!
j
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
@Jacques Smuts with RxJava you can use RxKoltin (it has same helpers for that) or enable new type inference