Skip to main content

Call me Santa

Posted by forax on December 17, 2006 at 7:11 AM PST

Since my last post, i've played with javac enought to be able to provide a patch that enables to let the compiler infers the type of local variables.

Life is a matter of choices

If you have already read the last Peter Ahé's blog entry you know that, at least, two proposed syntaxes compete.
The first one suggested by James Gosling and named 'Algol' in the prototype use ':=' instead of '=' to declare local variables. I've tweak the example widely used to explain generics and autoboxing without declaring the type of the local variables.

  // print a frequency table of words
  public static void main(String[] args) {
    map:=new HashMap<String,Integer>();
    for(word:args) {
      freq:=map.get(word);
      map.put(word,(freq==null)?1:freq+1);
    }
    System.out.println(map);
  }

Note that the compiler infers local variable in foreach loop too.

The second syntax supported by Peter Ahé and Christian Plesner Hansen use the keyword final to acheive the same goal. My running example with the 'final' syntax:

  public static void main(String[] args) {
    final map=new HashMap<String,Integer>();
    for(final word:args) {
      final freq=map.get(word);
      map.put(word,(freq==null)?1:freq+1);
    }
    System.out.println(map);
  }

How to use the prototype ?

To enable the Algol syntax, call javac in this way:
java -jar prototype-1.7-b04.jar -XDinferLocally=Algol TestAlgol.java

or if you prefer the final syntax:
java -jar prototype-1.7-b04.jar -XDinferLocally=final TestFinal.java

What you can do ?

You can test the prototype and let us know what you thinking.
Do you prefer the Algol syntax, the final syntax or none of both (perhaps because you hate the idea behind) ?

Download the prototype : prototype-1.7-b04.jar
Download the prototype patch against the Open JDK Compiler (1.7b04): patch-1.7b04

Cheers, Rémi

Related Topics >>