https://kotlinlang.org logo
e

eygraber

08/02/2023, 5:05 PM
I just tried enabling K2 in my Android project, and there's an issue where the compiler infers that the type of a SAM function is a nested class from an imported Java type instead of the imported SAM interface (code in 🧵 ). Is this known, or should I file a new issue?
My code looks like this:
Copy code
import android.view.View
import androidx.core.view.OnApplyWindowInsetsListener
import androidx.core.view.ViewCompat

suspend inline fun MyCustomView.foo(
  crossinline predicate: (view: View) -> Boolean = { it is EditText }
) {
  val listener = OnApplyWindowInsetsListener { _, insets ->
    ...
  }

  ViewCompat.setOnApplyWindowInsetsListener(this, listener)
}
The androidx code is as follows:
Copy code
public interface OnApplyWindowInsetsListener {
    @NonNull
    WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets);
}

public class ViewCompat {
    public static void setOnApplyWindowInsetsListener(@NonNull final View v,
            final @Nullable OnApplyWindowInsetsListener listener) {
        if (Build.VERSION.SDK_INT >= 21) {
            Api21Impl.setOnApplyWindowInsetsListener(v, listener);
        }
    }
}
but I get an error on the line where I call `setOnApplyWindowInsetsListener`:
Copy code
Argument type mismatch: actual type is android/view/View.OnApplyWindowInsetsListener but androidx/core/view/OnApplyWindowInsetsListener? was expected
The
android.view.View.OnApplyWindowInsetsListener
definition is:
Copy code
public class View {
    public interface OnApplyWindowInsetsListener {
        @NonNull
        WindowInsets onApplyWindowInsets(@NonNull View var1, @NonNull WindowInsets var2);
    }
}
If I fully qualify
OnApplyWindowInsetsListener
as
androidx.core.view.OnApplyWindowInsetsListener
then there is no error. I also tried using an import alias for
android.view.View
but that didn't help.
i

Ivan Kubyshkin [JetBrains]

08/03/2023, 6:10 AM
Can you attach a sample project?
e

eygraber

08/03/2023, 6:11 AM
I'll put one together tomorrow
🙏 1
Running
gradle assembleDebug
should result in the error
i

Ivan Kubyshkin [JetBrains]

08/04/2023, 7:42 AM
Thank you! Looks similar to https://youtrack.jetbrains.com/issue/KT-60172. Please file an issue - https://kotl.in/issue.
3 Views