December 09, 2002

JSR 201 thoughts

Java
My daugther Miruna (10) swimming 100 breast at Pleasanton, California swimming meet on December 8, 2002

As I pointed out in previous threads, I think the JSR 201 should broaden the scope of the proposed extensions. Instead of proposing just some minor syntactic sugars to the Java syntax, it should try to extend the language in such a way that allows unlimited extensibility of the syntax by means of macros.

The idea of macros comes from Lisp languages, which use this to successfully extend the semantic of the language. In Lisp the core runtime engine deals with very few abstractions, everything else is implemented through macros.

A very interesting macro system written for Java is Java Syntactic Extensions (JSE) from MIT, which allows the language to be extended by developers, with no need for a JSR. The scope of JSR 201 for example is already implemented in few lines of code in JSE and is completely portable. No need for compiler or VM changes to support them.

Many of the proposed extensions being talked on Java blogs these days, like functions (with or without closures), blocks and many other syntactic sugars can be easily implemented with a macro facility, as demonstrated by JSE.

Here are some examples taken from the JSE distribution:

  • enums
      public enum PowerWarning BATTERY_NORMAL, BATTERY_LOW, BATTERY_CRITICAL;
    
  • asserts, which are completely portable, no need to upgrade to JDK 1.4 to use them:
        public void yuk (boolean x) {
            assert(x, "hello");
        }
    
  • foreach:
        static void tst (List c, List l) {
            forEach (Integer i in c, Float f in l)
                System.out.println("contents: " + i + " and " + f);
        }
    
  • when:
        static void tst (boolean b) {
            when (b) { panic(); }
        }
    

JSE works by preprocessing the extended source code and generating pure Java source code which runs on any Java platform. There is no need for any compiler change or JVM change. Very beautifully done I think!

The beaty of this approach is that one can extend the syntax of the language to support C# annotations, functions , blocks or anything else which is needed using macros and the existing Java language.

Posted by ovidiu at December 09, 2002 01:02 PM |
 
Copyright © 2002-2011 Ovidiu Predescu.