Overload resolution ambiguity function reference requiring local var


#1

I am getting an expected overload resolution ambiguity issue when I do a function reference on an overloaded Java method (again, as expected). For my use case (adding an extension method on KFunction) I can’t really do a forwarding lambda and I was hoping to avoid a local var just to help the compiler out.

Are there any plans or issues I can watch for compile-time type ascription inside an expression as opposed to on assignment?

Also, how do I turn a function reference to a KFunction so I can invoke my extension method? For example, say I have the following extension function:

val <R> KFunction<R>.declarer: Class<*> get() = this.javaMethod!!.declaringClass

Now I want to get that for ByteBuffer.put(ByteArray!, Int, Int): ByteBuffer. How would I do this? I see the reflect method but that doesn’t feel right, but it looks like this:

val temp: ByteBuffer.(ByteArray, Int, Int) -> ByteBuffer = ByteBuffer::put
val temp2 = temp.reflect()?.declarer

Obviously this is just an example to demonstrate my problem. Any suggestions?

EDIT:

I got a way to make the type I want without a local var I suppose:

inline fun <T> forceType(fn: T) = fn
val fn = forceType<ByteBuffer.(ByteArray, Int, Int) -> ByteBuffer>(ByteBuffer::put)

I still don’t know how to get a safe KFunction out of that like I easily could if it weren’t an overload I was referencing. Help appreciated.


#2

For anyone else wondering, to get a KFunction from an overload, reflect() will not get it, it will get an anon lambda. You can simple cast to KFunction. So I now have this:

inline fun <T : Function<*>> forceFnType(fn: T) = fn as KFunction<*>
val fn = forceFnType<ByteBuffer.(ByteArray, Int, Int) -> ByteBuffer>(ByteBuffer::put)

This seems to solve my problem.


#3

I’m having the same issue here.
Thanks for your solution, cretz, but I would appriciate a less complicated way.
Are there any plans?