Matthew Layton
Matthew Layton

Reputation: 42390

Kotlin - KFunction3 unresolved reference

val fooCtor = ::Foo

The inferred type of fooCtor is KFunction3<Int, Int, Int, Foo>

When I try to explicitly define something as KFunction3<...>, for example

val fooCtor: KFunction3<Int, Int, Int, Foo> = ::Foo

The IDE/compiler tells me that KFunction3 is an unresolved reference.

Why is this? - I tried adding kotlin-reflect to my gradle.build file but no luck.

Upvotes: 2

Views: 1498

Answers (2)

pixix4
pixix4

Reputation: 1351

Through KFunction3 is not part of the standard lib you have to manually import it.

import kotlin.reflect.KFunction3

This solved your problem for me.

Upvotes: 4

gil.fernandes
gil.fernandes

Reputation: 14641

I have tried to reproduce your problem with IntelliJ IDEA 2017.2.6 with:

fun main(args: Array<String>) {
    fun foo(x: Int, y: Int, z: Int) = x % 2 != 0
    val f = ::foo
    val f1: KFunction3<Int, Int, Int, Boolean> = ::foo
}

This compiles without any problem.

How come that the return type is Foo in KFunction3? Does your Foo function return itself?

Update 1:

I have tested the same with a constructor with no errors:

class Foo (x: Int, y: Int, z: Int) {}

fun main(args: Array<String>) {
    val ft1 = ::Foo
    val ft2: KFunction3<Int, Int, Int, Foo> = ::Foo
}

Using Kotlin 1.2.31 and no extra libraries.

Upvotes: 1

Related Questions