Coder-Man
Coder-Man

Reputation: 2531

Why can't an annotation class infer type arguments?

Let's consider this code :

@Target(AnnotationTarget.FIELD)
annotation class DeserializeInterface<T: Any>(val targetClass: KClass<T>)

interface A
class B : A

class Test {
    @DeserializeInterface<B>(B::class) val a: A = B()
}

at line X I write @DeserializeInterface<B>(B::class), and Kotlin doesn't allow me to write just @DeserializeInterface(B::class).

Why doesn't it infer type arguments in this case from B::class?

In contrast, type inference works here just fine:

class AClass<T: Any>(val a: List<T>)

fun main(args: Array<String>) {
    val a = AClass(listOf(1,2,3))
}

but why?

Upvotes: 5

Views: 123

Answers (1)

Alexey Belkov
Alexey Belkov

Reputation: 2559

It is a limitation in the current type inference algorithm. I have filed an issue you can follow.

Upvotes: 3

Related Questions