简体   繁体   中英

Problems with sealed class hierarchies in Kotlin

I am following the MVI pattern in Android. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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