Issue with higher order function as a binding adapter

后端 未结 4 667
野的像风
野的像风 2021-02-07 10:09

I am running into issues trying to take a function as a parameter in a binding adapter using Kotlin/Android databinding. This example code throws e: error: cannot generate

4条回答
  •  生来不讨喜
    2021-02-07 10:38

    Use function: Runnable instead of function: () -> Unit.

    Android's data-binding compiler generates java code, to which, your kotlin function's signature looks like void testFunction(), as kotlin adapts Unit as void when calling from java.

    On the other hand, () -> Unit looks like kotlin.jvm.functions.Function0 which is a function which takes 0 inputs and returns Unit.INSTANCE.

    As you can see these two function signatures don't match, and that's why the compilation fails.

提交回复
热议问题