IllegalArgumentException when invoking [SOLVED]


Hi everyone,

I am playing with kotlin trying to build a parser generator. you can have a look at the current state on

I am currently struggling with an IllegalArgumentException when calling a

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(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at kotlin.reflect.jvm.internal.FunctionCaller$Method.callMethod(FunctionCaller.kt:98)
	at kotlin.reflect.jvm.internal.FunctionCaller$
	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 =, 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 ?




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

var v: Any? = Visit(n)

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>
else {
	var v: Any? = Visit(n)

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