IllegalArgumentException when invoking KFunction.call [SOLVED]


#1

Hi everyone,

I am playing with kotlin trying to build a parser generator. you can have a look at the current state on https://github.com/b3b00/KLY

I am currently struggling with an IllegalArgumentException when calling a KFunction.call

the full stack trace is

Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at kotlin.reflect.jvm.internal.FunctionCaller$Method.callMethod(FunctionCaller.kt:98)
	at kotlin.reflect.jvm.internal.FunctionCaller$InstanceMethod.call(FunctionCaller.kt:115)
	at kotlin.reflect.jvm.internal.KCallableImpl.call(KCallableImpl.kt:107)
	at net.b3b00.kly.parser.visitor.SyntaxTreeVisitor.Visit(SyntaxTreeVisitor.kt:45)
	at net.b3b00.main.MainLexerKt.testVisitor(MainLexer.kt:68)
	at net.b3b00.main.MainLexerKt.main(MainLexer.kt:89)

My call is

var result: Any? = null;
//some code here to populate args
result = function.call(configuration.ParserInstance, args)

And my called function is the following :

@Production("primary: INT")
    public fun Primary(intToken : Token<ExpressionToken>?): Int//intToken : Token<ExpressionToken> ) : Int
    {
        if (intToken != null) {
            return intToken.IntValue
        }
        return 0
    }

I assume that somehow kotlin cannot convert the Any? (as in args:List<Any?>) to a Token
But I cant get how to solve it.
I tried to change my Primary function to accept an Any? parameter but then I cant convert the parameter to Token<*> to get my usefull data.

Could you help me ?

thanks,

Olivier


#2

i solved my problem. It was a mistake when building my args list.
I used to do something like

var v: Any? = Visit(n)
 args.add(v)

But it happened sometimes need to return a Token value. In this particular case kotlin lost the value type and replaced it by a Unit. As I can know when Visit will return a token i changed my code to

if (n is SyntaxLeaf<T>) {
	var t: Token<T> = Visit(n) as Token<T>
	args.add(t)
}
else {
	var v: Any? = Visit(n)
}

might be the cost of a (very) strongly typed language