Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 5 Oct 2000 12:49:05 -0400 (EDT)
From:      Chris BeHanna <behanna@zbzoom.net>
To:        FreeBSD-Java <java@FreeBSD.ORG>
Subject:   Re: Sun Keynote at JavaCon2000 - C++ templates
Message-ID:  <Pine.BSF.4.21.0010051051590.49343-100000@topperwein.dyndns.org>
In-Reply-To: <00e801c02e90$75f47fe0$0302010a@biohz.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, 4 Oct 2000, Renaud Waldura wrote:

> How are we going to create those generic classes by name? It doesn't
> seem to be addressed in the spec. What if the parameterized type
> doesn't exist yet?

    Specializations of a template are created by a compiler on an
as-needed basis.  Typically, what is done in C++ is that you #include
the header file that defines the template class, and the specialized
prototypes are generated based upon what uses your compilation module
has for the template.  Some C++ compilers will look for a template
implementation in the same location as the header file, while others
require you to #include that, too.

    Java does not have a preprocessor, per se; however, an import
specification that imports the template class will allow javac (or
gjc) to generate the specializations required for the uses that the
given compilation module has for the template.

Example:

--- file HashMap.java

public class<S,T> HashMap implements Map {
    //... ctors and stuff go here

    public void add( S key, T obj) { ... }

    public T get( S key) { ... }

    // and so on
}

--- file Fred.java

import java.util.Map;
import java.util.HashMap;

public class Fred {
    Map map = new HashMap<String,String>();

    public static void main(String[] args) {
        Fred f = new Fred();

        for( int i=0; i<(args.length)/2; i+=2) {
            f.add( args[i], args[i+1]);
        }
    }
}


    That's a contrived example, to be sure.  In this case, a
specialization of HashMap<S,T> to HashMap<String,String> is generated,
which then becomes a first-class type.  Its add method is specialized
to

    public void add( String key, String obj)

and its get method is specialized to

    public String get( String key)

This way, the compiler can catch any problems with trying to pass
parameters of the wrong type or trying to assign to values of the
wrong type.

    Yes, these errors can creep into large systems when the left hand
is not aware of what the right is doing, and vice-versa.  It's just
one more thing to help you with correctness of your software, and,
because the Java can know in advance that the specialization is of a
specific type, more efficient bytecode can be generated (and, perhaps,
the JIT can generate more efficient native code).

> I don't know about this... It really looks like to me that in effect
> parameterized classes are meta-classes, and maybe they should be identified
> as such. How about a meta class type, that you can instantiate with your
> parameter type the regular way?
> 
> It sounds like a parameterized class is a class describing potentially many
> very different classes: a meta class. Just like classes specify instances
> with different fields, meta classes specify classes with different types.
> "Instantiate" a meta class to get a class parameterized to your type.
> 
> It feels like we're about to introduce this strange new citizen into the
> language, the "parameterized class", which is not really a class, but
> somehow is.

    This sounds to me more like a nomenclature problem.  In C++ land,
the addition of templates allowed people to get away from using awful
DECLARE() macros.  Although we don't have a macro preprocessor in
Java, someone could easily use the C preprocessor to do the same
thing, if they really had to (I even thought of doing this myself, to
avoid the ugliness and lack of run-time type safety that ensues from
having to cast everything back from Object when using Java
collections).

--
Chris BeHanna
Software Engineer (at yourfit.com)
behanna@zbzoom.net




To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-java" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0010051051590.49343-100000>