Hello, I'm trying to add a function to a class but...
# compiler
a
Hello, I'm trying to add a function to a class but is adding it as static. Is there any way to remove the static modifier? Code in comment
Copy code
context(IrPluginContext)
private fun IrClass.createBuildLambdaFunction(mutableClass: IrClass) {
    val buildFun = irFactory.buildFun {
        name = Name.identifier("build")
        returnType = irBuiltIns.unitType
        origin = IrDeclarationOrigin.DEFINED
        visibility = DescriptorVisibilities.PUBLIC
    }.apply {
        parent = this@createBuildLambdaFunction

        val blockParameter = buildValueParameter(this) {
            name = Name.identifier("block")
            type = irBuiltIns.functionN(2).typeWith(
                mutableClass.defaultType,
                this@createBuildLambdaFunction.defaultType,
                irBuiltIns.unitType,
            )
        }

        valueParameters += blockParameter
    }

    buildFun.body = irFactory.createBlockBody(UNDEFINED_OFFSET, UNDEFINED_OFFSET) // TODO
    declarations += buildFun
}
t
What do you mean by
static
? You can try to dump the class to see what is actually built:
println(yourClass.dump())
I typically check the dump and compare it to the dump of some actual code I wrote manually to see the difference.
a
I'm using jadx-gui to check what the result is. This is the result of jadx-gui
Copy code
public static final void build(@NotNull Function2<? super MutableTestClass, ? super TestClass, Unit> function2) {
    Intrinsics.checkNotNullParameter(function2, "block");
}
and this the result of dump:
Copy code
FUN name:build visibility:public modality:FINAL <> (block:kotlin.Function2<sample.TestClass.MutableTestClass, sample.TestClass, kotlin.Unit>) returnType:kotlin.Unit
  VALUE_PARAMETER name:block type:kotlin.Function2<sample.TestClass.MutableTestClass, sample.TestClass, kotlin.Unit>
  BLOCK_BODY
t
Your function is missing the dispatch receiver parameter (at least). Something like this:
Copy code
irFactory.buildFun { 
            
}.also {
     it.dispatchReceiverParameter = irGet(irClass.thisReceiver!!)
}
❤️ 1
a
that was it! thank you very much!
👍 1