christophsturm
03/18/2021, 5:43 PMQuincy
03/18/2021, 5:48 PMQuincy
03/18/2021, 5:48 PMval implementor: IImpl = /* some code that returns IImpl */
val zeroArityRef = { implementor.method() }
val twoArityRef = { a, b -> implementor.method(a, b) }
Quincy
03/18/2021, 5:50 PMwbertan
03/18/2021, 5:57 PMval m: IImpl.() -> Unit = IImpl::method
val m2: IImpl.(Int, String) -> Unit = IImpl::method
christophsturm
03/18/2021, 5:59 PMVampire
03/18/2021, 6:01 PMchristophsturm
03/18/2021, 6:05 PMVampire
03/18/2021, 6:06 PM"foo"::toString
then it is the reference to the function toString
on the instance "foo"
.
In the first half of your example, the ::method
is a reference to the function method
called on the instance this
.
In the second half of your example you try to get a function reference to an abstract function on an interface, so which receiver should this be called on if invoked?
See, that's the problem.
What wbertan
showed works, because with the IImpl.
at the type you say "this will be called on some actual instance of IImpl
so no receiver is necessary yet.
What you had in your original example could be called m()
, what wbertan showed needs to be called myInstanceOfIImpl.m()
.christophsturm
03/18/2021, 6:08 PMchristophsturm
03/18/2021, 6:08 PMinterface IImpl {
fun method()
fun method(number: Int, name: String)
}
// this does not work
val m3: IImpl.()->Unit = IImpl::method
val m4: IImpl.(Int, String)->Unit = IImpl::method
function(m4 as KCallable<*>)
fun function(callref: KCallable<*>) {println(callref)}
christophsturm
03/18/2021, 6:08 PMchristophsturm
03/18/2021, 6:10 PMfunction(IImpl::nonOverloadedMethod)
christophsturm
03/18/2021, 7:09 PMchristophsturm
03/18/2021, 7:10 PMVampire
03/18/2021, 10:06 PM() -> Unit
is a send-contained function not taking any arguments and returning nothing that you can call like you have it, so it needs a receiver already as part of the reference. IImpl.() -> Unit
is the reference to a method that you can call on an instance of IImpl
, so the receiver is not part of the reference.christophsturm
03/18/2021, 10:33 PMIImpl::foo
to a function that takes a kcallable.Vampire
03/18/2021, 11:19 PMcallref
, then it will not work with m3
or m4
unless you also give an instance of IImpl
as argument so that it has a receiver.christophsturm
03/18/2021, 11:22 PMVampire
03/18/2021, 11:24 PMchristophsturm
03/18/2021, 11:26 PM