Hello, I want to add a parameter to function expr...
# compiler
a
Hello, I want to add a parameter to function expressions:
val f={}
->`val f={i:Int-> }` ,I tried this:
Copy code
moduleFragment.transformChildrenVoid(object :
    IrElementTransformerVoidWithContext() {
    override fun visitFunctionExpression(expression: IrFunctionExpression): IrExpression {
        expression.function.addValueParameter {
            name= Name.identifier("i")
            type=pluginContext.referenceClass(ClassId.fromString("<http://kotlin.Int|kotlin.Int>"))?.owner?.defaultType!!
        }

        return super.visitFunctionExpression(expression)
    }
})
but got an error:
Copy code
Caused by: java.lang.IllegalArgumentException: No argument for parameter VALUE_PARAMETER name:a index:0 type:<http://kotlin.Int|kotlin.Int>:
CALL 'public final fun invoke (a: <http://kotlin.Int|kotlin.Int>): kotlin.Unit declared in <root>.FileKt.<clinit>.<no name provided>' type=kotlin.Unit origin=BRIDGE_DELEGATION
  $this: GET_VAR '<this>: <root>.FileKt.<clinit>.<no name provided> declared in <root>.FileKt.<clinit>.<no name provided>.invoke' type=<root>.FileKt.<clinit>.<no name provided> origin=null
I'm new to kotlin compiler plugin, and I'm confused as the function expression is not invoked,so where should I pass the argument ? Or the way I add the paramter is wrong?
d
Seems like you forgot to update the type of original expression (
IrFunctionExpression
) Note that after such transformation you need to update the type of each call-site of
f
(and type of
f
itself too)
a
Oh, thank you