1

I have two data classes e.g.


sealed class API1Status {
    data class Success(
        val name: String
    ) : API1Status()

    sealed class Error : API1Status() {
        object API1FAiled : Error()
        object SystemError : Error()
        object ServerError : Error()
        object InvalidRequest : Error()
    }
}

sealed class API2Status {
    data class Success(
        val name: String
    ) : API2Status()

    sealed class Error : API2Status() {
        object API2FAiled : Error()
        object SystemError : Error()
        object ServerError : Error()
        object InvalidRequest : Error()
    }
}

As you have noticed, I have 3 errors common between both sealed classes, namely.

object SystemError : Error()
object ServerError : Error()
object InvalidRequest : Error()

I would like to know how to extract these errors out of the sealed classes to be used across the sealed classes.

Nik
  • 2,913
  • 7
  • 40
  • 66
  • 1
    Create an interface that they all implement. – Tenfour04 Mar 31 '22 at 14:36
  • Yeah, those objects have nothing in common with each other as far as the type system is concerned - one lot is a type of ``API1Status.Error`` and the others are ``API2Status.Error``s, and there's no connection between those classes - they just happen to have the same names and patterns to their hierarchies. An interface (or three different interfaces) is the easiest way to give them a shared type – cactustictacs Mar 31 '22 at 19:09

0 Answers0