简体   繁体   English

Kotlin 中密封类层次结构的问题

[英]Problems with sealed class hierarchies in Kotlin

I am following the MVI pattern in Android.我在 Android 中遵循 MVI 模式。 I have the following code:我有以下代码:

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>()
}

However, when I move the UseCaseResult class into its own file, I get an error:但是,当我将 UseCaseResult 类移动到它自己的文件中时,出现错误:

Cannot access <init>: it is private in UseCaseResult

How to solve this?如何解决这个问题?

Yes, an error will be thrown if subclasses of sealed class are not in the same file or not as nested subclasses.是的,如果密封类的子类不在同一个文件中或与嵌套子类不同,则会引发错误。

refer this for further details: Sealed classes inside another class in Kotlin can't be compiled: cannot access '<init>' it is private有关更多详细信息,请参阅: 无法编译 Kotlin 中另一个类中的密封类:无法访问 '<init>' 它是私有的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM