Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference to KFunctionN

I am trying to create function references in Kotlin, my understanding is I should be able to access the generated KFunctionN interfaces at compile time, but I am getting an 'Unresolved reference' compilation error for KFunction1<T, R>.

For example:

inline fun <reified T : Any, reified R : Any> passFn(someFn: KFunction1<T, R>) {
    //consume someFn
}

I have kotlin-reflect on the class path and working fine. Any ideas what I am missing?

I am using Kotlin 1.3.21 and IntelliJ 2018.3.

like image 837
jimmy_terra Avatar asked Apr 26 '26 12:04

jimmy_terra


2 Answers

This looks like an IDE issue, I have typed the import the import kotlin.reflect.KFunction1 and compiler error goes away.

However, if you add an expression/statement which returns a KFunction1 type and explicitly declare the type, the Intellij imports it correctly. For example;

"\\s+".toRegex()::matches

Can be extracted as

val kFunction1: KFunction1<@ParameterName(name = "input") CharSequence, Boolean> = "\\s+".toRegex()::matches
like image 56
Laksitha Ranasingha Avatar answered Apr 28 '26 11:04

Laksitha Ranasingha


Ok, very simple fix for this - you need to manually add the KFunctionN import, IntelliJ won't resolve it automatically.

like image 23
jimmy_terra Avatar answered Apr 28 '26 10:04

jimmy_terra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!