Conditional method in sealed class for returning same type
05:47 13 Mar 2026

I got this sealed class:

sealed interface ResponseResult {
    data class Ok(val value: T) : ResponseResult
    data class Response(val response: ResponseEntity) : ResponseResult
}

My goal is to write a method which returns a ResponseEntity for both cases. This should only be possible/available when Ok = Ok>.

I know in Swift/Rust I can write a conditional conformance method: a method which is only available when T conforms to a specific type (in this case: ResponseEntity). I was wondering if something was possible in Kotlin as well. The ResponseEntity in the Ok case can hold a subclass of Any.

kotlin