I really not have understood why you cannot use of if(DEF_FOO) paradigm.
Consider:
DEF_FOO == false for pre Java 2
DEF_FOO == true for Java 2
if (DEF_FOO)
{
// pre Java 2 only code here
}
else
{
// Java 2 only code here
// this code does not compile when compiled with an
// early Java compiler
}
My customer wants the same code to run with Java 2 and with pre Java 2
runtimes. I've tried your solution with the Symantec Cafe 3.0 compiler.
It won't compile the code (even though static analysis shows that it
will never execute the non-compiling code because of the if () clause
directing it to the pre-Java 2 code).
Another problem. Assuming a release mode compiler will optimize out the
none-executable portion, a debug mode compiler won't optimize, so you
still have the problem.
In any case:
Who prevents you to write #ifdef in your Java sources
and pre-process your Java sources via a C pre-processor?
Too much effort. Support should be part of the language. Maintenance
should be thought of at the language design stage.
Another problem with Java, which is a design and maintenance issue:
Lack of support for enumerations, unless thats been added for Java 2
(and I've missed it - which is possible, given that I mainly write C++).
Problem is the code I'm writing needs to be compilable with pre Java 2
compilers. So even if enumerations have finally been added to Java I
can't use them.
The clumsy, none-type-safe workaround is defining lots of public final
integer constants in your class. A slightly clumsier workaround is
defining lots of public final integer constants in their own class. Why
can't I just define them as an enumeration in whichever class I see fit?
The type safety restrictions are in force at compile time, the JVM would
still see these as integers and treat them as such.
What could be simpler than
enum enumerationName
{
enumValue1,
enumValue2,
enumValue3
};
Note, no need for public, final, static, integer or assignment of any
value (although the ability to do the latter may be useful in some
places).
compared to
class myEnumerationClass
{
public static final integer enumValue1 = 1;
public static final integer enumValue2 = 2;
public static final integer enumValue3 = 3;
};
and there is *still* no actual concept at the language level that these
three integers are part of the same enumeration. They are just integers
which can be assigned to any other integer. If they were enumerations
you'd be get a type safety error from the compiler if you tried to
assign them to a different enumeration type.
There is plenty of space in the Java instruction set for support of
this, if they wanted to handle enumerations as a special type. I don't
think they'd need to do that. It could all be handled at the compiler
level.
I fail to see how forcing clumsy representations of simple concepts
makes writing software easier or aids maintainability.
</aaah! endRant>
I think #ifdef and lack of enums annoy me more than anything else about
Java. Lack of enumerations really really annoys me. There is just no
excuse for a modern language, especially an object oriented one
supposedly promoting type safety, its like going back to assembly
language, where everything is integers.
Stephen