Skip to content

Instantly share code, notes, and snippets.

@mikesamuel
Last active July 11, 2024 06:08
Show Gist options
  • Save mikesamuel/ba02df18f943156062ec2af726619015 to your computer and use it in GitHub Desktop.
Save mikesamuel/ba02df18f943156062ec2af726619015 to your computer and use it in GitHub Desktop.
Lexically nested types

In JavaScript a nested type can read and assign variables from outer scopes.

function counter(start = 0) {
  let n = start;
  class Counter {
    peek() { return n; }
    increment() { ++n; }
  }
  return new Counter();
}

In Java, a nested type can read effectively final locals, but cannot read or set mutable locals because the Java language people are loathe to convert portions of stack frames to objects, or reify stack frames in the JVM.

Counter counter(int start) {
  int n = start;
  class BadCounterImpl implements Counter {
    public int peek() { return n; } // OK
    public void increment() { ++n; } // ILLEGAL
  }

  // OK
  class CounterImpl implements Counter {
    // Capture by copy of effectively final int start
    private int n = start;
    ...
  }
  return new CounterImpl();
}
@ShawSumma
Copy link

These types exposed by returns and that have no fully qualified name are called Voldemort types because you cant say their name (At least D calls them that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment