Hi all, I have a question about kotlin (v1.3.31) r...
# announcements
l
Hi all, I have a question about kotlin (v1.3.31) reflection.
Copy code
fun main(args: Array<String>) {
    fun <P1, P2, R> ((P1, P2) -> R).partial(p1: P1): (P2) -> R = { p2: P2 -> this(p1, p2) }

    fun regexMatch(re: Regex, s: String): Boolean = re.containsMatchIn(s)
    val match = ::regexMatch.partial(Regex("^123"))

    println(match("12345"))
    println(match("123xx"))
    println(match("2345"))
    println(match)
    println(match.reflect()!!.run {
        "(${parameters.map { it.type }.joinToString()}) -> $returnType" })
}
The output (the signature of
match
) is
(P2) -> R
. Is there any way to infer the actual correct signature which should be
(String) -> Boolean
?
d
Generics are erased at runtime, so in this case the answer is no. If you can make
partial
inline
you could mark all the type parameters as
reified
, in which case you can (maybe) get the correct signature.
l
inline/reified
doesn't help.
d
Then you can't do anything. Why do you need to find out?
l
I need to mark down the function signatures to implement dynamic function type casting in kotlin-scripting. It seems that I need figure out a workaround. Thanks!