Java with Parameterized Types
|
The PolyJ language includes support for generic programming in the
form of parameterized types. PolyJ provides constrained parametric polymorphism,
like some other proposals for adding
genericity to Java.
Unlike some, it use signature constraints, which provide flexibility when composing a program.
PolyJ also allows all types to be used as parameters, even basic types
like int
.
Another powerful feature is that instantiation types and parameter types
are first-class types
that may be used wherever a type may be used -- particularly, in safe run-time
casts and with "instanceof". See the FAQ for more details on these issues.
The PolyJ language is defined in a paper appearing in POPL'97. Almost all of the language defined there is now implemented. We are considering further extensions.
PolyJ is easy to fit into an existing Java development environment, since it is backwards compatible with Java. Since PolyJ works by translation, it has several nice compatibility properties:
The compiler is built as an extension to the guavac compiler developed by David Engberg. It is written in C++. Because it is built on a version of guavac that does not support inner classes, the current PolyJ compiler does not either. However, future releases of PolyJ will be implemented in Java and will support inner classes and other extensions.
The PolyJ compiler works by translating PolyJ code into corresponding Java code, in a manner sketched in the POPL'97 paper. In this translation, we avoid the code expansion associated with implementations of the C++ template mechanism. The PolyJ compiler generates very little extra code for each instantiation of a parameterized abstraction. In the translation, the code of a parameterized class is translated into a similar Java class. For each instantiation, a small trampoline class is generated to glue the generic code to its parameters. The translation uses only very straightforward, well-supported Java features. It does not use the Java Reflection API and it does not require an extended JVM. The translation imposes a small or negligible performance overhead for most programs.
Documentation |
|