Re: "Real" Templates for Java?

From: Daniel Bonniot (Daniel.Bonniot_at_inria.fr)
Date: 07/22/04


Date: Thu, 22 Jul 2004 14:36:34 +0200


> Does anybody know a tool for java that does "real" templates

First, your requirements are incompatible with the definition of
Java-the-language, so I'm assuming what you are looking for is a
Java-like language that supports those features, while compiling to Java
bytecodes and generally assuring compatibility with Java code.

One close match for your requirements seems to be PolyJ
(http://www.pmg.lcs.mit.edu/polyj/). They offer more power than Java
1.5. Note however that they use an homogeneous translation, so you won't
avoid the casts nor the boxing/unboxing for primitive types.

A big set back is that development seems to have died in July 2000,
while there are still important bugs and missing features
(http://www.pmg.lcs.mit.edu/polyj/bugs.html), including no inner classes
and no full support of JDK 1.1.

Another project (with which I'm involved) to look at is the Nice
language (http://nice.sf.net).

Regarding your requirements:

> - Avoiding of runtime typechecks / casts altogether.

The translation is homogeneous, so the runtime checks are still there. I
don't know of any tool that does the expansion. That should be done in
the future, especially for primitive types, where it really makes a
difference.

> - Availability of the actual type parameters inside the template
> instances (so you can e. G. do a
> T[] blubb = new T[100];
> which impossible with Generics.

new T[...] is possible in Nice.

> - Signature-Based templating (we write a template that accepts all
> objects that have a foobar() method, and only when using the template,
> you have to ensure that your Class/Interface parameter you use for
> instantiation has this foobar() method. This allows to use the same
> template (in different places) for different types that have no common
> parent providing the foobar() method.

Nice uses type constraints, since we believe it allows for better design
and documentation. Still, it can handle using the same generic code with
different types developed independently. That's possible because you can
define "abstract interfaces", and make existing classes (imported from a
package you don't control) implement those interfaces.

I refer you to the relevant part of the user manual for a detailed
motivating example:
http://nice.sourceforge.net/manual.html#abstractInterfaces

> - Runs in java 1.4 VMs, as it will take some time for 1.5 to get stable
> enough.

It does.

> - Possibility of templating primitive types (currently, this is not so
> important for us, as the number of primitive types is rather limited and
> won't expand in the near future :-).

You can use primitive types as type parameters. However, you must be
aware that in the current implementation this will possibly cause
boxing/unboxing.

> Hints to both "preprocessor" and direct byte-code creating tools are
> welcome, but we prefer open source tools (or at least tools which come
> together with their source code and the license allows us to modify them
> and fix bugs).

Nice compiles directly to java bytecode. It is actively maintained and
developed, and it is available under the GPL. It has many other
improvements, like anonymous functions/closures, multimethods, tuples,
optional parameters to methods, and method contracts (as in Design by
contract)... All this is designed to be as seamlessly inter operable
with Java as possible. Find out more at http://nice.sf.net/ and feel
free to ask further questions about it.

Cheers,

Daniel



Relevant Pages

  • Re: Generics are cool
    ... >> Tim Tyler wrote: ... >>> of Java. ... >>> never to have had primitive types in the first place. ... > that having everything be an object involves a performance hit is ...
    (comp.lang.java.programmer)
  • Re: newbie lost in strings......
    ... When you do int i = 0; you're declaring a variable i that holds the integer ... holds a reference to an Integer object which holds the integer value 0. ... In some cases, the variable is one of the primitive types (byte, char, int, ... The reason Java includes ...
    (comp.lang.java.help)
  • Re: Question of JAVA programmer to C# programmers
    ... > all primitive types in Java have the same semantics (aside ... with "fixed" length (as int, char, etc in C or Java), as opposed to ... In Java only variables for the primitive types can be of value types, ... while the rest are reference types. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: newbie lost in strings......
    ... not the Java sense) is usually an 8 bit ... > holds a reference to an Integer object which holds the integer value 0. ... > In some cases, the variable is one of the primitive types (byte, char, int, ... > i" both declare variables. ...
    (comp.lang.java.help)