December 10, 2002

More on Java macros

Crab on Stinson Beach, California

Continuing the thread on Java language extensibility, Carlos comments on a private email I sent to Joshua Bloch and Gilad Bracha, the spec leads of JSR 201, with Carlos, James and Ken CC-ed on the email, regarding the use of JSE-like macros added to Java.

He points out that we should break the problem in two parts. The first is what should be the syntax used to define macros, and the second what is the syntax the macros are allowed to extend.

Carlos also points out that macro usage allow for the introduction of new keywords to the language, which will soon end up in name conflicts. He argues that we need to introduce namespaces for all these new keywords.

For the first part I agree that we need to discuss the issues involved. Personally I think the way JSE defines macros is good, although it may be hard to comprehend at times, especially for non-Lisp programmers. It's definitely not going to be the usual easy to read Java code, since the reader has to change his/her typical Java procedural thinking. As for the language extensibility part, I think the current definition is good, I cannot think of more ways to extend it or of possible shortcomings.

When it comes to keyword namespaces, I think we should avoid introducing these, or at least we should avoid having to introduce them in the keyword names. IMO this is ugly and has the potential of introducing changes in the compiler (which should be avoided at all costs because of high costs/impossibility in backporting these). I haven't found this to be an issue while programming Scheme or Lisp, and I don't think this could be a real issue with Java.

Another good point raised by Carlos is that macros cannot address things like autoboxing, which is a new feature proposed by JSR 201. I definitely agree. I think the scope of JSR 201 should be reduced to extending the language syntax only in ways which cannot be addressed by macros, currently autoboxing and static imports. The rest of the extensions (enumerations and enhanced for loop) should be defined as macros part of a standard library of macros, addressed either by this JSR or a different one.

A last comment I have is that the macros implementation should be easily backported to older versions of Java, so we can easy their adoption and ease the migration to this new feature.

Posted by ovidiu at December 10, 2002 11:28 AM |
Copyright © 2002-2016 Ovidiu Predescu.