dimanche 12 janvier 2020

KCallable doesn't work in Kotlin when one of the parameters is vararg

I have an issue where I have a class that stores a KCallable<Any> and calls it using parameters passed in by an interpreter that's running a custom scripting language. This works for most functions when I use KCallable.call(), but it doesn't seem to properly handle functions with a vararg parameter, instead assuming that the parameter is an array of the given type. Is there any way I can work around this issue using some sort of reflection method to convert the input for the parameters? Here's my current code:

class KotlinFunction(function: KCallable<Any>) {
    fun call(args: List<Any>) {
        function.call(*args.toTypedArray())
    }
}

given the function as class member:

fun concat(vararg xs: String) = xs.reduce{l,r -> l + r}

Using 3 parameters, I get the following error:

java.lang.IllegalArgumentException: Callable expects 1 arguments, but 3 were provided.





Aucun commentaire:

Enregistrer un commentaire