Can I add an existing java class to a sealed Kotlin class?

117 views Asked by At

I am using 2 classes to handle error statuses, the Spring's own org.springframework.http.HttpStatus and my custom ErrorStatus:

enum class ErrorStatus(val code: Int, val reasonPhrase: String) {
    ELEMENT_NOT_FOUND(1404, "Element not found"),
    UNKNOWN_ERROR(1000, "Unknown Error"),
}

I would like to seal both classes using:

sealed interface Error

It's simple to do this with my class: enum class ErrorStatus(val code: Int, val reasonPhrase: String) : Error {

But is it possible to mark HttpStatus class as a part of this sealed interface?

1

There are 1 answers

0
Tenfour04 On

No, you cannot apply an interface to a class externally. Also, members of a sealed class or interface have to be defined inside the same project module as the sealed class or interface.

I would wrap the other class.

data class HttpStatusError(val status: HttpStatus): Error

You might consider making it an inline wrapper class:

@JvmInline value class HttpStatusError(val status: HttpStatus): Error