Nice to @Share?
Peter Levart
peter.levart at marand.si
Tue Feb 23 08:23:09 PST 2010
On Tuesday 23 February 2010 14:43:32 David.Moss at ubs.com wrote:
> > By the way, I am very happy with final (or implicit final).
> > When I code and find that I need a non-final local variable,
> > I first try to rewrite the code to avoid to use non-final
> > before trying to use a mutable object.
>
> Where des this leave closures?
>
> i.e.: what would we need to make the following code do what it is meant to:
>
> public class C {
> public #int() getSequenceGenerator(int start, int increment) {
> return #int() {
> int ret = start;
> start += increment;
> return ret;
> };
> }
> }
>
>
Well, there is a workarround if you want to capture arbitrary state by copy and still not have to define any extra classes or stash each wariable in it's own wrapper:
public class C {
public #int() getSequenceGenerator(final int start, final int increment) {
return new Object() {
int seq = start;
final #int() fn = #() {
try { return seq; } finally { seq+=increment; }
}
}.fn;
}
}
The benefit being that it is obvious what code does...
Peter
More information about the lambda-dev
mailing list