Austin Paquette
07/22/2021, 9:38 PMclass Example(val id: Int) {
companion object {
fun <T, V> demo(property: KProperty1<T, V>, value: V): V? {
return null
}
}
}
// this works
val test1 = Example.demo(Example::id, 0)
// this works, but shouldn't pass validation
val test2 = Example.demo(Example::id, "1")
The idea is that I want to be able to infer the type of the passed-in property (in this case I would expect Int
. I feel like I'm close but not understanding something. Any help (or even suggestions of what to Google) would be awesome.Landry Norris
07/22/2021, 9:44 PMAustin Paquette
07/22/2021, 10:07 PMLandry Norris
07/22/2021, 10:10 PMAustin Paquette
07/22/2021, 10:10 PM"1"
to provide an error.Landry Norris
07/22/2021, 10:12 PMAustin Paquette
07/22/2021, 10:15 PMAustin Paquette
07/22/2021, 10:28 PM<>
values comment, that made me start thinking about specifying them and led to this idea.
Not sure how it will do but I'm okay with the results because it stops the build rather than relying on runtime exception throwing.Sourabh Rawat
07/22/2021, 10:41 PMwhen is
construct. Not sure if it will work.Austin Paquette
07/22/2021, 11:15 PMwhen is
be placed? I'm not sure were it's intended to go.Austin Paquette
07/22/2021, 11:17 PMwhen (value) {
is Int -> handleInt(value)
else -> error("Unsupported value type: ${value::class}")
}
Sourabh Rawat
07/23/2021, 1:21 AMYoussef Shoaib [MOD]
07/25/2021, 11:43 PMYoussef Shoaib [MOD]
07/25/2021, 11:44 PM@kotlin.internal.Exact
annotation to simply limit the parameters from causing the type param to be Any?Youssef Shoaib [MOD]
07/25/2021, 11:47 PM@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
import kotlin.reflect.*
class Example(val id: Int) {
companion object {
fun <T, V> demo(property: KProperty1<T, @kotlin.internal.Exact V>, value: @kotlin.internal.Exact V): V? {
return null
}
}
}
fun main() {
// this works
val test1 = Example.demo(Example::id, 0)
// this works, but shouldn't pass validation
val test2 = Example.demo(Example::id, "1")
}
Austin Paquette
07/26/2021, 5:18 AM@kotlin.internal.Exact
annotation. Is that a byproduct of the suppression? It works interestingly well but I'd be a bit hesitant to use it because it seems like this is a bug though and the behavior could break at any point ?Austin Paquette
07/26/2021, 5:25 AMYoussef Shoaib [MOD]
07/26/2021, 9:31 AMExact
annotation is simply by its package and name, and so you can use this trick to shadow the annotation, and this shadowing is much more reliable and "supported", if you will, than suppressing the error.Youssef Shoaib [MOD]
07/26/2021, 9:34 AMopen 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) {}
fun main(args: Array<String>) {
test(Foo::barInt, 12345) // ok
test(Foo::barInt, "NotInt") // ok! Type T is Any, but I would like compile error
}