Syntax...

Jakob Praher jp at hapra.at
Sun Nov 22 09:46:13 PST 2009


Daer Neal,

thanks for your reply.

Neal Gafter schrieb:
> On Sat, Nov 21, 2009 at 9:18 AM, Jakob Praher <jp at hapra.at 
> <mailto:jp at hapra.at>> wrote:
>
>     Why did you change to # for introducing function types and
>     creating closures?
>
>
> Because the # syntax for references to methods is already part of the 
> Java mindset from its use in javadoc.  That naturally generalizes to a 
> corresponding syntax for lambdas and therefore function types.
IMHO it is not. # for me stems from the fact that javadoc renders a 
class as a page and uses HTML anchor names (as URL fragments) to 
identify subelements of that page.
# is also used for fields e.g.: 
http://java.sun.com/javase/6/docs/api/java/lang/System.html#err

Furthermore it is already used heavily having different meaning in other 
languages:
* Shell scripts and a lot of scripting languages use it as a comment
* It is often used as preprocessor escape (not only in C/C++/C#)
* The Java Server Faces expression language uses it as value binding 
(denoting more or less evaluation/dereferencing, not closure creation)

>     But I think having to write:
>
>       int[] result = x.select( #(int c) c % 2 == 0)
>
>     is harder to read. Don't you think?
>
>
> Not especially.
>
Accpeted. It is subjective. Personally I have difficulties with that 
syntax. Also note that the D language (which is syntactically quite 
similar to Java) is able to create anonymous functions/closures in the 
following way [1]:

    FunctionLiteral:
        function Typeopt ParameterAttributes opt FunctionBody
        delegate Typeopt ParameterAttributes opt FunctionBody
        ParameterAttributes FunctionBody
        FunctionBody

    ParameterAttributes:
        Parameters
        Parameters FunctionAttributes

Which means the following is also valid in D:

    int abc(int delegate(long i));

    void test()
    {   int b = 3;

        abc( (long c) { return 6 + b; } );
    }

Plus the ability to omit the the argument list, if there are no parameters:

    double test()
    {   double d = 7.6;
        float f = 2.3;

        void loop(int k, int j, void delegate() statement)
        {
        for (int i = k; i < j; i++)
        {
            statement();
        }
        }

        loop(5, 100, { d += 1; } );
        loop(3, 10,  { f += 3; } );

        return d + f;
    }



--Jakob

[1] http://www.digitalmars.com/d/2.0/expression.html#FunctionLiteral


More information about the closures-dev mailing list