YACS - yet another closures syntax

Peter Levart peter.levart at gmail.com
Mon Jul 14 01:31:41 PDT 2008


(a re-post to the list - forgot to cc:)

Oh yeah, exceptions!

If trying to mimic method syntax, then it would look like this:

{ long => void throws InterruptedException } wait = { long millis =>
Thread.sleep(millis); };

vs.

(long) throws InterruptedException {void} wait = (long millis) {
Thread.sleep(millis); };

but then it feels so "disjoint" so that one does not know where the
construct starts and where it ends (the established syntax is better
in this respect). So maybe half/half:

(long){void throws InterruptedException} wait = (long millis) {
Thread.sleep(millis); };

... I don't know. An evening idea doesn't seem clever at all in the morning...

Peter

On Mon, Jul 14, 2008 at 1:31 AM, Neal Gafter <neal at gafter.com> wrote:
> Where do you put the throws clause?
>
> On Sun, Jul 13, 2008 at 3:16 PM, Peter Levart <peter.levart at gmail.com> wrote:
>> 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