[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scriptometer: measuring the ease of SOP (Script Oriented Programming) of programming languages

Anton van Straaten wrote:
> You can use anonymous inner classes to achieve this in Java, e.g. given an
> interface like:
>         interface UnaryPredicate { boolean apply(Object o); }
> ...you can implement code to support this sort of thing:
>         someCollection.find(new UnaryPredicate() {
>                 public boolean apply(Object elem) {
>                         return elem.someProperty() == someMatchValue;
>                 }});
This sort-of-works in Java.  If you're used to letting anonymous
functions close over anything they can see in the current lexical
environment, Java will surprise you when the compiler tells you it can't
close over non-final variables.  If someMatchValue in the code above is
a vanilla local variable, it won't compile.

Supposedly the reason for this restriction has something to do with
threads - without it, threads would need full access to non-local
lexical environments.

Robert Bane
Global Science & Technology
7855 Walker Drive, Suite 200
Greenbelt MD 20770
301-474-9696 FAX:301-474-5970