Discussion: Closures
Patrick Niemeyer <[email protected]> Thu, 3 Feb 2005 10:31:55 -0600
| Newsgroups | gmane.comp.java.beanshell.devel |
|---|---|
| Message-ID | <[email protected]> |
One thing that people often ask about is closures. Technically
BeanShell namespaces and method closures have always let you do more or
less arbitrary things, but there is no convenience syntax for using
these features.
One possibility would be to use another eval-like method. For example,
it could be named callable(). It could return a closure with a call()
method, and the whole thing might map to the Java type
java.util.concurrent.Callable where appropriate. This would primarily
be a delayed evaluation form of eval() that could take additional
leading arguments, etc.
x = callable( foo=someMethod(); print("hello"); );
y = callable(
foo=someMethod();
bar++;
print("hello");
);
z=callable( foo, bar,
print(foo);
someMethod(bar)
);
x.call();
y.call();
z.call( "foo", "bar" );
The alternative (as discussed) would be to use the more concise { }
block-like syntax that we've discussed before. My only fear there is
that it could become ambiguous with Java syntax.
For example:
c={ someMethod(); }
Is that a closure or is it just a loose form of:
Object [] c = { someMethod() };
Technically the semi-colon makes it a statement and we can
differentiate the two. But from a human standpoint it is a little hard
to tell at first whether this is some kind of array/list syntax or a
closure.
Maybe the { } syntax is useful enough to warrant it. Is the appeal of
closure delicate enough that it's lost by having to type a few extra
characters? ;) Maybe so.
Please send feedback.
thanks,
Pat
-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl