All codes tested on this playground.
There will be something defferect in Java.
We mock a closure with method inner an Anonymous Sub-Class.
In Java you can't write a value like that:
<A> Function<A, A> idiot = a -> a ; /// err
var idiot = (<A> Function<A, A>) (a -> a) ; /// err
You can just:
<A> A idiot (A a) { return a ; }
/// or
final var birds =
new Object()
{
<A> A idiot (A a) { return a ; }
} ;
birds.idiot(7) ; // 7
The generic just allowed at method but not a lambda.
So, we can just let the method be a value !!
Like these:
we define:
final var things =
new Object()
{
Integer a = 7 ;
String b = "bzzzz" ;
<A> Function<A,A> idi = (<A> Function<A,A>) (a -> a) ;
} ;
and want:
things.a ; // 7
things.b ; // "bzzzz"
things.idi.apply(things.a) ; // 7
things.idi.apply(things.b) ; // "bzzzz"
but we cannot, because the <A> Function<A,A> idi = (Function<A,A>) (a -> a) ;
is unsupported in Java (yet now), it will give error because of this line.
But we can do things like that now: all method just use to return a value.
Like that:
final var things =
new Object()
{
Integer a () { return 7 ;}
String b () { return "bzzzz" ;}
<A> Function<A,A> idi () { return (Function<A,A>) (a -> a) ;}
} ;
And then so we can use:
things.a() ; // 7
things.b() ; // "bzzzz"
things.idi().apply(things.a()) ; // 7
things.idi().apply(things.b()) ; // "bzzzz"
Things is done !!
And that's all given by anonymous (sub)classes' supports in Java.
final var things =
new Object()
{
Integer a = 7 ;
String b = "bzzzz" ;
<A> Function<A,A> idi () { return (Function<A,A>) (a -> a) ;}
} ;
things.a ; // 7
things.b ; // "bzzzz"
things.idi().apply(things.a) ; // 7
things.idi().apply(things.b) ; // "bzzzz"
... means only lambda values into method style.
Or called modules. That is simple, also a new Object () {...}
, we have no necessary to use a IIFE even ...
I don't know how to define (or mock) a recursive type in Java yet ...