sealed class Result { data class Success(val data: T) : Result() data class Unauthorized(val exception: Exception) : Result() data class Timeout(val exception: Exception) : Result() data class Error(val exception: Exception) : Result() } fun Result.map(mapper: (A) -> B): Result { return when (this) { is Success -> Success(mapper(data)) is Unauthorized -> Unauthorized(exception) is Timeout -> Timeout(exception) is Error -> Error(exception) } }