YACS - yet another closures syntax
Peter Levart
peter.levart at gmail.com
Sun Jul 13 15:16:11 PDT 2008
I have seen similar proposal on an old Wiki page here:
http://wiki.java.net/bin/view/JDK/ClosuresSyntaxInJava7 and I might not be
the first to come up with this, but anyway. Here it is (as examples writen
below the current established syntax):
{=>void} a = {=> System.out.println("Hello World!"); }
(){void} a = (){ System.out.println("Hello World!"); }
{int => boolean} f = {int x => y <= x && x <= z};
(int){ boolean } f = (int x){ y <= x && x <= z };
{String, String => String} concat = {String s1, String s2 => s1 + s2};
(String, String){ String } concat = (String s1, String s2){ s1 + s2 };
public {T => U} converter({=> T} a, {=> U} b, {T => U} c) {
return {T t => a.invoke().equals(t) ? b.invoke() : c.invoke(t)};
}
public (T){U} converter((){T} a, (){U} b, (T){U} c) {
return (T t){ a.invoke().equals(t) ? b.invoke() : c.invoke };
}
//
// For restricted, just use () instead of {}...
(String)(int) len = (String s)(s.length());
(String, String)(String) concat = (String s1, String s2)(s1+s2);
... my comments are:
- the number of keystrokes is the same as with currently established syntax
- using this syntax it might be easier to visually separate parameters from
closure expression/body
- the parameters (or parameter types) in parentheses intuitively "suggest"
that they are parameters.
- they expression/body (or return type) in braces suggest this is closure body
(or what that expression/body returns - a return type)
- using second pair of parentheses instead of braces suggest the body is more
of an expression than a procedure (restricted closures)
What dou you think?
Peter
-
More information about the closures-dev
mailing list