Can this overload resolution ambiguity be resolved?

I’m working on a DSL using builders. The following methods in one of the builders result in overload ambiguity.

infix fun <R: Any> KProperty1<T, R?>.by(block: Builder<R>.() -> Unit) { ... }

and

infix fun <R: Any> KProperty1<T, List<R>?>.by(block: Builder<R>.() -> Unit) { ... }

Code like this: Person::children by { ... } produces this ambiguity. Is there a way to resolve this, other than renaming one of the methods?

2 Likes

I can’t solve the problem…

I can tell how to rename only the JVM function: @JvmName.
(the JVM can’t have two of the same funcion-names)

I can tell you how to do it based upon the generic of the lambda you’re passing in: @BuilderInference.

fun main(){
    //these call other functions
    Test::lst by {a : String -> }
    Test::lst by {a : List<String> -> }
}

//and as code
@JvmName("de")
infix fun <R: Any> KProperty1<*, List<R>?>.by(@BuilderInference block: (R) -> Unit) { println("list") }
infix fun <R: Any> KProperty1<*, R?>.by(@BuilderInference block: (R) -> Unit) { println("single") }

class Test(
    val lst : List<String> = emptyList()
)

But I can’t answer your question…

Interesting. This looks like a nice feature, thanks for pointing this out.