P
P
pqgg7nwkd42018-06-13 16:58:02
Kotlin
pqgg7nwkd4, 2018-06-13 16:58:02

How to prevent Any type inference when using interface with "out X" parameter in Kotlin?

I'll show you with an example:

open class Foo(var barInt: Int)

// public interface KProperty1<T, out R> : KProperty<R>, (T) -> R

fun <E : Foo, T> test(field: kotlin.reflect.KProperty1<E, T>, value: T): T = TODO()

fun main(args: Array<String>) {
    test(Foo::barInt, 123) // ок
    test(Foo::barInt, "NotInt") // тоже ок! Тип T = Any, но я бы хотел ошибку компиляции
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pqgg7nwkd4, 2018-06-13
@pqgg7nwkd4

The answer why this can not be done yet, and a temporary solution is written here:
https://discuss.kotlinlang.org/t/how-prevent-type-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question