Created
March 7, 2022 00:44
-
-
Save wangyung/f17a0a50d5939e4b6037992a15877233 to your computer and use it in GitHub Desktop.
kotlin inner closure example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun countingLambda(): () -> Int { | |
var counter = 0 | |
val incrementalCounter: () -> Int = { | |
counter += 1 | |
counter | |
} | |
return incrementalCounter | |
} | |
// decompiled java | |
@NotNull | |
public static final Function0<Integer> countingLambda() { | |
void counter; | |
Ref.IntRef intRef = new Ref.IntRef(); | |
intRef.element = 0; | |
Function0 incrementalCounter2 = (Function0)new Function0<Integer>((Ref.IntRef)counter){ | |
final /* synthetic */ Ref.IntRef $counter; | |
public final int invoke() { | |
++this.$counter.element; | |
return this.$counter.element; | |
} | |
{ | |
this.$counter = intRef; | |
super(0); | |
} | |
}; | |
return incrementalCounter2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment