不使用inline的匿名函数会多次修改外部变量,因为这种lambda表达式是一个类的实例.
kotlin:
funmain(args:Array){? ? println("Hello World!")varl =50;vartest3L = {l1:Int->? l++? ? println("test3l Program arguments:${l}")}// Try adding program arguments via Run/Debug configuration.// Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html.? test(args, test3L)test(args, test3L)// test3()}privatefuntest(args:Array, test3P: (Int)->Unit){? ? test3P(1)}
java:
import kotlin.Metadata;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.Ref;
import org.jetbrains.annotations.NotNull;
@Metadata(
? mv = {1, 9, 0},
? k = 2,
? d1 = {"\u0000 \n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\u0010\b\n\u0002\b\u0002\u001a\u0019\u0010\u0000\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005\u001a/\u0010\u0006\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u00032\u0012\u0010\u0007\u001a\u000e\u0012\u0004\u0012\u00020\t\u0012\u0004\u0012\u00020\u00010\bH\u0002¢\u0006\u0002\u0010\n¨\u0006\u000b"},
? d2 = {"main", "", "args", "", "", "([Ljava/lang/String;)V", "test", "test3P", "Lkotlin/Function1;", "", "([Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V", "untitled2"}
)
public final class MainKt {
? public static final void main(@NotNull String[] args) {
? ? ? Intrinsics.checkNotNullParameter(args, "args");
? ? ? String var1 = "Hello World!";
? ? ? System.out.println(var1);
? ? ? final Ref.IntRef l = new Ref.IntRef();
? ? ? l.element = 50;
? ? ? Function1 test3L = (Function1)(new Function1() {
? ? ? ? // $FF: synthetic method
// $FF: bridge method? public Object invoke(Object var1) {
? ? ? ? ? ? this.invoke(((Number)var1).intValue());
? ? ? ? ? ? return Unit.INSTANCE;
? ? ? ? }
? ? ? ? public final void invoke(int l1) {
? ? ? ? ? ? int var10001 = l.element++;
? ? ? ? ? ? String var2 = "test3l Program arguments: " + l.element;
? ? ? ? ? ? System.out.println(var2);
? ? ? ? }
? ? ? });
? ? ? test(args, test3L);
? ? ? test(args, test3L);
? }
? private static final void test(String[] args, Function1 test3P) {
? ? ? test3P.invoke(1);
? }
}