Skip to content

Instantly share code, notes, and snippets.

@wangyung
Created March 7, 2022 00:44
Show Gist options
  • Select an option

  • Save wangyung/f17a0a50d5939e4b6037992a15877233 to your computer and use it in GitHub Desktop.

Select an option

Save wangyung/f17a0a50d5939e4b6037992a15877233 to your computer and use it in GitHub Desktop.

Revisions

  1. wangyung created this gist Mar 7, 2022.
    29 changes: 29 additions & 0 deletions InnerClosureExample.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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;
    }