This is a simple class that store result or exception, but I can’t find a best way to handle those two fields without null-check at once. If we can use Contracts inside a class or even Contracts description can accept val field, that will be nice.
class ResultOrException<out R : Any, out E : Throwable> {
constructor(result: R) {
this.result = result
this.exception = null
}
constructor(exception: E) {
this.result = null
this.exception = exception
}
@JvmField
val result: R?
@JvmField
val exception: E?
}
val not allowed in Contracts description. (Of couse this make no sense, the field is nullable.)
fun <R : Any, E : Throwable> ResultOrException<R, E>.result(): R? {
contract {
//Error in contract description: only references to parameters are allowed in contract description
returns(null) implies (exception != null)
}
return result
}