christophsturm
03/19/2021, 10:20 AMimport kotlin.reflect.KCallable
interface IImpl {
fun method()
fun method(number: Int, name: String)
fun nonOverloadedMethod()
}
fun function(callref: KCallable<*>) {println(callref)}
function(IImpl::nonOverloadedMethod)
function(IImpl::method) // resolution ambiguity
I wonder how a syntax that makes that work could look likeelizarov
03/19/2021, 4:35 PMKCalleble<*>
type in your function
. What are you really trying to do?christophsturm
03/19/2021, 4:56 PMelizarov
03/19/2021, 5:00 PMchristophsturm
03/19/2021, 5:01 PMYoussef Shoaib [MOD]
03/19/2021, 11:09 PMval
in the middle to force only one of the overloads to be used like this:
val func: (Int, String) -> Unit = IImpl::method
val func2: () -> Unit = IImpl::method
function(func)
function(func2)
elizarov
03/20/2021, 11:21 AMKCallable<*>
this way.Youssef Shoaib [MOD]
03/20/2021, 12:02 PMimport kotlin.reflect.KCallable
import kotlin.reflect.KFunction3
import kotlin.reflect.KFunction1
interface IImpl {
fun method()
fun method(number: Int, name: String)
fun nonOverloadedMethod()
}
fun function(callref: KCallable<*>) {println(callref)}
fun main() {
val func: KFunction3<IImpl, Int, String, Unit> = IImpl::method
val func2: KFunction1<IImpl, Unit> = IImpl::method
function(IImpl::nonOverloadedMethod)
function(func)
function(func2)
println(func == func2)
}
christophsturm
03/20/2021, 1:18 PMchristophsturm
03/20/2021, 1:18 PMfun <A,B> f(function: KFunction1<A,B>) = function
fun <A,B,C,D> f(function: KFunction3<A,B,C,D>) = function
function(f<IImpl, Unit>(IImpl::method))
function(f<IImpl, Int,String,Unit>(IImpl::method))
christophsturm
03/20/2021, 1:19 PMcastFun
fun <A,B> castFun(function: KFunction1<A,B>) = function
fun <A,B,C,D> castFun(function: KFunction3<A,B,C,D>) = function
function(castFun<IImpl, Unit>(IImpl::method))
function(castFun<IImpl, Int,String,Unit>(IImpl::method))
christophsturm
03/20/2021, 1:20 PMYoussef Shoaib [MOD]
03/20/2021, 1:21 PMcastFun1
and castFun3
so that the type parameters are automatically inferredchristophsturm
03/20/2021, 2:03 PMval mock = mock<IImpl>()
mock.function()
mock.overloadedFunction()
mock.overloadedFunction("string")
mock.overloadedFunction(10)
val calls = getCalls(mock)
expectThat(calls).containsExactly(
call(IImpl::function),
call(IImpl::overloadedFunction),
call(IImpl::overloadedFunction, "string"),
call(IImpl::overloadedFunction, 10)
)
christophsturm
03/20/2021, 2:04 PM