S
Sebastian
Someone made a passing comment in another thread to the effect that Java
8 lambdas ("closures") were nothing but syntactic sugar for (anonymous)
inner classes. Here are a three differences:
1) lambdas can only instantiate "functional" (single abstract method)
interfaces
2) lambdas have different scoping rules ("this" means different things)
3) lambdas perform better due to the use of the invokedynamic JVM
instruction. Type representation, instance creation, and invocation
strategy are decided at runtime. This offers optimization potentials,
because evaluating a lambda expression need not create a new instance
each time. There could be „shared lambdas“ through use of a constant
static method in a constant pool, needing no new class and/or new object.
This is quite a bit more than what I'd call "syntactic sugar".
-- Sebastian
8 lambdas ("closures") were nothing but syntactic sugar for (anonymous)
inner classes. Here are a three differences:
1) lambdas can only instantiate "functional" (single abstract method)
interfaces
2) lambdas have different scoping rules ("this" means different things)
3) lambdas perform better due to the use of the invokedynamic JVM
instruction. Type representation, instance creation, and invocation
strategy are decided at runtime. This offers optimization potentials,
because evaluating a lambda expression need not create a new instance
each time. There could be „shared lambdas“ through use of a constant
static method in a constant pool, needing no new class and/or new object.
This is quite a bit more than what I'd call "syntactic sugar".
-- Sebastian