https://kotlinlang.org logo
Title
n

natario1

04/01/2023, 3:59 PM
Hi guys, I have the following interface:
interface Bindable<in T> {
    fun bind(input: T)
    fun unbind()
}
And I’m trying to define two utility functions, one when T is nullable, one when it is not:
inline fun <B : Bindable<T>, T: Any> B.use(input: T, block: B.() -> Unit) {
    bind(input); try { block() } finally { unbind() }
}
inline fun <B : Bindable<T?>, T: Any> B.use(input: T? = null, block: B.() -> Unit) {
    bind(input); try { block() } finally { unbind() }
}
The only difference being that, in case of nullable
T
I can provide a default value to input. The problem is that the compiler complains about resolution ambiguity at use site in some cases, I think due to the
in
modifier. Is there any mechanism (annotation?) I can use to help it choose? For example, I’m calling
Bindable<Long?>.use(0L) { … }
. I don’t care which one is chosen in this case, source code is the same.
Oh, I can just remove
input
from the second, adjust super types and it works as expected.