Need help with Generics, contravariance needed
Sorry that I’m giving a longer example, but I think it becomes clearer what I want to do:
fun interface Pattern {
fun test(obj: A): Boolean
}
class Matcher(private val obj: A) {
private var result: R? = null
fun otherwise(default: () -> R) = result ?: default()
infix fun Pattern<A>.then(value: () -> R) {
if (result == null && this.test(obj)) {
result = value()
}
}
}
fun match(obj: A, body: Matcher.() -> R): R =
Matcher(obj).run(body)...