FunctionImpl is deprecated

While checking kotlin lambda decompiled code in java, found out Function1.kt interface is implemented by FunctionImpl abstract class which is deprecated. If it is deprecated, which one i should look into for correct java implementation of Kotlin Lambda ?

Original Kotlin code

fun performDbOperation(operation: (db: DbInstance) -> Unit) {
        val dbInstance = DbInstance()
        dbInstance.open()
        operation.invoke(dbInstance)
        dbInstance.close()

    }

Decompiled java code

public final void performDbOperation(@NotNull Function1 operation) {
      Intrinsics.checkParameterIsNotNull(operation, "operation");
      DbInstance dbInstance = new DbInstance();
      dbInstance.open();
      operation.invoke(dbInstance);
      dbInstance.close();
   }

I’m not sure what your question is? Do you want to know how to call performDbOperation from java?
In that case you can use

performDbOperation(new Function1<DbInstance, Unit>() {
    override public Unit invoke(DbInstance db){
        // TODO
        return Unit.INSTANCE;
    }
});

I’m pretty sure you can also use javas lambda syntax (can’t remember the syntax though, it’s been a while since I used java).

Also FunctionImpl is something internal. Not sure what it was used for. I think it was used to provide lambdas with more than 22 parameters. If you want to see a decompiled version of a lambda implementation just decompile any kotlin code that passes a lambda to a function. Make sure the function is not inline though (otherwise it won’t work). The code should look somewhat like I posted above. There is probably some extra for capturing local variables but the essence is the same. Each lambda is compiled into a class implementing FunctionX with the lambda code in the invoke function.

1 Like