The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


Alex Winston

Alex Winston is a Senior Software Engineer with ComFrame Software Corporation located in Nashville, TN. He focuses on emerging open source technologies and design patterns and is activaly involved in the Middle Tennessee Java Users Group. He regularly participates in discussion and design of various technologies within the java community and evangalizes the choice that java provides

 

Alex Winston's blog

Closure Syntax

Posted by alexwinston on February 27, 2008 at 11:00 AM PST
I would like to preface this entry with the fact that I am not suggesting a concrete syntax in the examples below. I am merely using the examples as talking points and illustrations of what I consider to be the "syntactical spirit" of the Java language.

I will be the first to admit that syntax is quite subjective and typically creates consternation within the context of programming specifically. However, there is also what could be considered the "syntactical spirit" of a language and in this regard I feel that BGGA and FCM have ostensibly missed the mark.

The most concerning illustration of this is from the FCM proposal itself, "Finally, it should be borne in mind that the syntax is relatively unimportant - the semantics of the proposal are what really matter". This statement in my opinion is one reason the Java community has been adverse to embrace the proposed changes. Inherently people are averse to change but if change is inevitable it should be familiar. Syntax is very important and can in some ways inhibit or foster the use of new functionality and to suggest that either proposal is familiar or similar to Java in most regards would be dubious at best.

I would like to state the fact that I do not take issue with the semantics of either proposal simply the syntax of each. That being said let me provide examples from each proposal.

BGGA

    {int=>int} plus2 = {int x => x+2};

I would like to ask honestly who in there right mind thinks this is consistent within any stretch of the imagination with the spirit of the Java language? Yes Java is verbose, yes it can be cumbersome, but it is not this. Now I am not going to purport to know the best syntax but wouldn't something like the following be more in line with the Java language?

    int plus2(int) = int (int x) { return x + 2; };

I have read similar statements from a number of individuals within the community and the discussion invariably turns to semantics while overlooking the importance of syntax.

The following example is even more disturbing.

    public {T => U} converter({=> T} a, {=> U} b, {T => U} c) {  
        return {T t => a.invoke().equals(t) ? b.invoke() : c.invoke(t)};  
    }

Not only that but something like the following appears more terse.

    public <T, U> U converter(T a(), U b(), U c(T)) {
        return (T t) { a().equals(t) ? b() : c(t); };
    }

Again this is just a talking point, and might very well not be syntactically equivalent, but as an avid supporter of the Java language I cannot support the BGGA syntax in any capacity.

FCM

Unfortunately FCM is not much better, they did put some thought into the syntax but it only makes for a marginally more spirited alignment.

    public class MyClass {
        public void process() {
            (void(String)) example = #(String message) {
                Logger.log(this, message);
            };
            example.invoke("Logged message");
        }
    }

It seems there are unneeded parentheses and calls to invoke. Something like the following seems more appropriate although less terse.

    public class MyClass {
        public void process() {
            void example(String) =  void (String message) {
                Logger.log(this, message);
            };
            example("Logged message");
        }
    }

One thing I am not sure I understand either is the need for the invoke(...) call. Is there a particular reason function(...) could not be used over function.invoke(...)?

All that being said I am not trying to create controversy. I am merely encouraging a reevaluation of the syntax proposed by both BGGA and FCM. This, in my opinion, will be as critical to the success of closures as semantics. It almost seems as though we should refocus BGGA and FCM on the semantics and create competing proposals for the syntax. I do realize that the syntax of each proposal follows the semantics, form follows function, but that is a moot point until we have something more palatable.
Related Topics >> Programming      
Comments
Comments are listed in date ascending order (oldest first)
Syndicate content