The prefix symbol : or ::

Collin Fagan collin.fagan at gmail.com
Tue Jun 21 04:57:22 PDT 2011


I prefer ':' I think it screams less "I'm a lambda" but still lets one know
there is something different going on.

I think : is used only for labels (used vary rarely, right?) the for each
loop and the ternary operator. I'm not as skilled with grammers as others on
this board but all other uses require something else to make it valid. You
need 'for', '?' or an identifier.

Required examples:

:(x) { return x + 1; }
:{x -> x + 1}

If  a single : introduces ambiguity then I suggest :: instead.

::(x) { return x + 1; }
::{x -> x + 1}

One could even use :: for method references.

personList.sort(::sortByLastName);

Or if it's static

personList.sort(Person::sortByLastName);


Collin
----

More Examples:

Syntax: Strawman with Single Colon

Generated examples

/*
 * Syntax Poll Example:
 */
list.filter( :(Foo t){t.length() > 3} )
    .map( :(Foo t){t.barCount} )
    .max();

/*
 * Student Filtering Multi-line:
 */
students.filter( :(student){
    if (student == null || student.getName() == null){
        return false;
    } else {
        return student.getName().equals("Smith");
    }
} );

/*
 * Student Filtering Single Line:
 */
students.filter( :(s){s != null && "Smith".equals(s.getName())});

/*
 * Case Insensitive Comparator: Assignment
 */
Comparator<String> CASE_INSENSITIVE_ORDER = :(String s1, String s2){
    int n1=s1.length(), n2=s2.length();
    for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) {
        char c1 = s1.charAt(i1);
        char c2 = s2.charAt(i2);
        if (c1 != c2) {
            c1 = Character.toUpperCase(c1);
            c2 = Character.toUpperCase(c2);
            if (c1 != c2) {
                c1 = Character.toLowerCase(c1);
                c2 = Character.toLowerCase(c2);
                if (c1 != c2) {
                    return c1 - c2;
                }
            }
        }
    }
    n1 - n2;
};


/*
 * Case Insensitive Comparator: Inline
 */
Collections.sort( :(String s1, String s2){
    int n1=s1.length(), n2=s2.length();
    for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) {
        char c1 = s1.charAt(i1);
        char c2 = s2.charAt(i2);
        if (c1 != c2) {
            c1 = Character.toUpperCase(c1);
            c2 = Character.toUpperCase(c2);
            if (c1 != c2) {
                c1 = Character.toLowerCase(c1);
                c2 = Character.toLowerCase(c2);
                if (c1 != c2) {
                    return c1 - c2;
                }
            }
        }
    }
    n1 - n2;
} );


/*
 * Action Listener: (Adapted from
org.apache.cocoon.forms.formmodel.RowActionDefinition)
 */
button.addActionListener( :(ActionEvent actionEvent){
    Repeater.RepeaterRow row =
Repeater.getParentRow(event.getSourceWidget());
    Repeater repeater = (Repeater)row.getParent();
    // Rotation: up in a table is left in a list!
    repeater.moveRowLeft(repeater.indexOf(row));
} );

/*
 * Action Listener: (Adapted from org.apache.batik.swing.JSVGCanvas)
 */
button.addActionListener( :(ActionEvent actionEvent){
    if (gvtRoot == null) {
        return;
    }
    AffineTransform rat = getRenderingTransform();
    if (at != null) {
        Dimension dim = getSize();
        int x = dim.width / 2;
        int y = dim.height / 2;
        AffineTransform t = AffineTransform.getTranslateInstance(x, y);
        t.concatenate(at);
        t.translate(-x, -y);
        t.concatenate(rat);
        setRenderingTransform(t);
    }
} );


More examples
Syntax: Strawman with Double Colon

Generated examples

/*
 * Syntax Poll Example:
 */
list.filter(::(Foo t){t.length() > 3})
    .map(::(Foo t){t.barCount})
    .max();

/*
 * Student Filtering Multi-line:
 */
students.filter(::(student){
    if (student == null || student.getName() == null){
        return false;
    } else {
        return student.getName().equals("Smith");
    }
});

/*
 * Student Filtering Single Line:
 */
students.filter(::(s){s != null && "Smith".equals(s.getName())});

/*
 * Case Insensitive Comparator: Assignment
 */
Comparator<String> CASE_INSENSITIVE_ORDER = ::(String s1, String s2){
    int n1=s1.length(), n2=s2.length();
    for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) {
        char c1 = s1.charAt(i1);
        char c2 = s2.charAt(i2);
        if (c1 != c2) {
            c1 = Character.toUpperCase(c1);
            c2 = Character.toUpperCase(c2);
            if (c1 != c2) {
                c1 = Character.toLowerCase(c1);
                c2 = Character.toLowerCase(c2);
                if (c1 != c2) {
                    return c1 - c2;
                }
            }
        }
    }
    n1 - n2;
};


/*
 * Case Insensitive Comparator: Inline
 */
Collections.sort(::(String s1, String s2){
    int n1=s1.length(), n2=s2.length();
    for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) {
        char c1 = s1.charAt(i1);
        char c2 = s2.charAt(i2);
        if (c1 != c2) {
            c1 = Character.toUpperCase(c1);
            c2 = Character.toUpperCase(c2);
            if (c1 != c2) {
                c1 = Character.toLowerCase(c1);
                c2 = Character.toLowerCase(c2);
                if (c1 != c2) {
                    return c1 - c2;
                }
            }
        }
    }
    n1 - n2;
});


/*
 * Action Listener: (Adapted from
org.apache.cocoon.forms.formmodel.RowActionDefinition)
 */
button.addActionListener(::(ActionEvent actionEvent){
    Repeater.RepeaterRow row =
Repeater.getParentRow(event.getSourceWidget());
    Repeater repeater = (Repeater)row.getParent();
    // Rotation: up in a table is left in a list!
    repeater.moveRowLeft(repeater.indexOf(row));
});

/*
 * Action Listener: (Adapted from org.apache.batik.swing.JSVGCanvas)
 */
button.addActionListener(::(ActionEvent actionEvent){
    if (gvtRoot == null) {
        return;
    }
    AffineTransform rat = getRenderingTransform();
    if (at != null) {
        Dimension dim = getSize();
        int x = dim.width / 2;
        int y = dim.height / 2;
        AffineTransform t = AffineTransform.getTranslateInstance(x, y);
        t.concatenate(at);
        t.translate(-x, -y);
        t.concatenate(rat);
        setRenderingTransform(t);
    }
});

On Tue, Jun 21, 2011 at 5:06 AM, Jack Moxley <jack at moxley.co.uk> wrote:

> how about the @ it might be used for annotations as well but it could quite
> happily sit along side.
>
> @(x) { return x + 1; }
> @{x -> x + 1}
>
>       _____
>
>  From: Stephen Colebourne [mailto:scolebourne at joda.org]
> To: lambda-dev [mailto:lambda-dev at openjdk.java.net]
> Sent: Tue, 21 Jun 2011 10:35:18 +0100
> Subject: Re: The prefix symbol
>
> Prefix character summary after 15 hours:
>
> Use a #
> #(x) { return x + 1; }
> #{x -> x + 1}
>
> Use a ^
> ^(x) { return x + 1; }
> ^{x -> x + 1}
>
> Use a \ (from another thread)
> \(x) { return x + 1; }
> \{x -> x + 1}
>
> (I'm not listing keywords that have been suggested elsewhere)
>
> Issues around typing the various characters on different keyboards
> have been raised.
>
> Once again, does anyone have a character they prefer more than one of
> the three above that they want to be considered? Remember, this isn't
> a vote, or a discussion thread, but a "gathering the options" thread.
>
> Stephen
>
>
> On 20 June 2011 18:54, Stephen Colebourne <scolebourne at joda.org> wrote:
> > The four syntax families split into two types, those with a prefix
> > symbol and those without.
> >
> > The prefix symbol is commonly mentioned as #:
> >
> > #(x) { return x + 1; }
> > #{x -> x + 1}
> >
> >
> > *** If you have a strong desire to see any symbol other than #
> > considered then please respond to this thread. ***
> >
> > - Your reply MUST specify the symbol
> > - Your reply MUST give a brief justification
> > - Your reply MUST repeat the two examples above using your preferred
> symbol
> > - You SHOULD try to ensure that your alternate symbol choice would
> > parse acceptably
> > - You MAY reply to suggest a keyword, however you should expect that
> > to be rejected
> >
> > Thread rules:
> > - Only reply if you prefer your alternate symbol to #
> > - To discuss something, change the thread title
> > - Don't reply just to say "I don't want a prefix symbol"
> > - Responding with a symbol suggestion doesn't preclude your first
> > choice actually being "no prefix symbol"
> >
> > For example, my preferred choice of prefix symbol is #, thus I should
> > not respond to this thread!
> >
> > Stephen
> > (this is an experiment to see if we can focus on one particular
> > discussion element at a time)
> >
>
>
>
>
>
>


More information about the lambda-dev mailing list