Aside from lambdas, Python has ‘inner functions’ where you just def inside a def. Java has anonymous inner classes and private functions, and Java 8 adds lambdas; I had to google this one, but apparently Java even has “local classes” which sounds like an exact match for what you want?
Lambdas in Java 8 can only access variables from the surrounding block as read-only. For example, if you want to calculate the sum of numbers between 1 and 100, this gives you a compile-time error:
int x = 0; IntStream.rangeClosed(1, 100).forEach(i → x += i);
If memory serves me well, in Pascal, local functions could also write to the variables they could see.
Aside from lambdas, Python has ‘inner functions’ where you just
definside adef. Java has anonymous inner classes and private functions, and Java 8 adds lambdas; I had to google this one, but apparently Java even has “local classes” which sounds like an exact match for what you want?Lambdas in Java 8 can only access variables from the surrounding block as read-only. For example, if you want to calculate the sum of numbers between 1 and 100, this gives you a compile-time error:
If memory serves me well, in Pascal, local functions could also write to the variables they could see.