Rakesh Patel
Rakesh Patel

Reputation: 9

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?

Upvotes: 0

Views: 2139

Answers (1)

Nataraj KR
Nataraj KR

Reputation: 1102

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

Upvotes: 3

Related Questions