【问题标题】:Problems with sealed class hierarchies in KotlinKotlin 中密封类层次结构的问题
【发布时间】:2020-03-30 17:32:49
【问题描述】:

我正在遵循 Android 中的 MVI 模式。我有以下代码:

sealed class AttendLeaveEventResult : UseCaseResult<AttendLeaveEventResult>(){
}
sealed class UseCaseResult<R> {
    open class SomeFetching<R> : UseCaseResult<R>()
    data class Success<R>(val result: R) : UseCaseResult<R>()
    data class Failure<R>(val error: Throwable) : UseCaseResult<R>()
}

但是,当我将 UseCaseResult 类移到它自己的文件中时,我得到一个错误:

Cannot access <init>: it is private in UseCaseResult

如何解决?

【问题讨论】:

标签: android generics kotlin


【解决方案1】:

是的,如果密封类的子类不在同一个文件中或与嵌套子类不同,则会引发错误。

更多详情请参考:Sealed classes inside another class in Kotlin can't be compiled: cannot access '<init>' it is private

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多