Kotlin 扩展函数无法在 java 中编译
Kotlin extension function failed to compile in java
我定义了以下扩展函数
fun <T> T.showAppPresentation(
appPresentable: Maybe<AppPresentable>,
appPresentationView: AppPresentationView,
closeListener: () -> Unit
) where T : Fragment, T : FragmentPresentable {
TODO()
}
其中 Fragment
是一个 android Fragment
而 FragmentPresentable
是一个接口。它在 Android Studio 中没有显示错误。当我尝试编译我的代码时,出现以下错误:
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()
^
我错过了什么?
我找到了解决方案:FragmentPresentable
位于名为 interface
的包中,该包是 java/kotlin 中的关键字。它在编译时引起了问题。我将 FragmentPresentable
移到包 view
中,现在一切正常。
我定义了以下扩展函数
fun <T> T.showAppPresentation(
appPresentable: Maybe<AppPresentable>,
appPresentationView: AppPresentationView,
closeListener: () -> Unit
) where T : Fragment, T : FragmentPresentable {
TODO()
}
其中 Fragment
是一个 android Fragment
而 FragmentPresentable
是一个接口。它在 Android Studio 中没有显示错误。当我尝试编译我的代码时,出现以下错误:
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()
^
我错过了什么?
我找到了解决方案:FragmentPresentable
位于名为 interface
的包中,该包是 java/kotlin 中的关键字。它在编译时引起了问题。我将 FragmentPresentable
移到包 view
中,现在一切正常。