Skip to content

Instantly share code, notes, and snippets.

@wangyung
Created March 7, 2022 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangyung/f17a0a50d5939e4b6037992a15877233 to your computer and use it in GitHub Desktop.
Save wangyung/f17a0a50d5939e4b6037992a15877233 to your computer and use it in GitHub Desktop.
kotlin inner closure example
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