Search code examples
kotlinkotlin-extension

Kotlin extension function failed to compile in java


I've defined the following extension function

fun <T> T.showAppPresentation(
    appPresentable: Maybe<AppPresentable>,
    appPresentationView: AppPresentationView,
    closeListener: () -> Unit
) where T : Fragment, T : FragmentPresentable {
    TODO()
}

where Fragment is an android Fragment and FragmentPresentable is an interface. It shows no error in Android Studio. When I try to compile my code, I got the following errors:

FragmentPresentableExtensionsKt.java:12: error: <identifier> expected
    public static final <T extends androidx.fragment.app.Fragment & FragmentPresentable>void showAppPresentation(@org.jetbrains.annotations.NotNull()
                                                                    ^
FragmentPresentableExtensionsKt.java:12: error: illegal start of type
    public static final <T extends androidx.fragment.app.Fragment & FragmentPresentable>void showAppPresentation(@org.jetbrains.annotations.NotNull()
                                                                                      ^
FragmentPresentableExtensionsKt.java:12: error: '(' expected
    public static final <T extends androidx.fragment.app.Fragment & FragmentPresentable>void showAppPresentation(@org.jetbrains.annotations.NotNull()
                                                                                       ^

What am I missing?


Solution

  • I found the solution: FragmentPresentable was inside a package named interface which is a keyword in java/kotlin. It caused the issue at compile time. I moved FragmentPresentable inside a package view and now everything is working as expected.