Generic Java

GenericJava is the WikiName for GJ, an extension of JavaLanguage which provides GenericTypes or templates.

Generics have been a long requested feature in Java. The hard part has been figuring out how to add them in a backwards compatible way. GJ was designed by GiladBracha?, MartinOdersky, DavidStoutmire?, and PhilipWadler.

GJ was the basis of Java 5's generics. (See PhilipWadler's GJ page at http://homepages.inf.ed.ac.uk/wadler/gj/ .) Time to update the page surely?
Unlike templates in C++, GJ does not cause code bloat. It exists mainly to add an additional measure of type safety, but the primary and very welcome effect is that the number of casts you end up writing falls to the floor. GJ also adds CovariantReturnTypes to Java, so you don't even have to cast the returns of your clone calls. -- MichaelFeathers


See: http://developer.java.sun.com/developer/technicalArticles/releases/generics/
A colleague and I have been writing some functionality for a speculative feature in our application using the new JavaGenerics prototype and the OmniCore? IDE. Our experience so far has led us both to the conclusion that generics (as presented in the collections framework) are going to be a great tool to add to our tool kit, but they are not going to change our world completely. Most of our day-to-day programming will not change all that much. I will still return typed arrays quite a bit instead of a typed collection, since most of the time all I want to provide is an unmodifiable "list" of objects anyway. I really look forward to having generics implemented fully in the Java SDK (and Aspects, too), but, like XML, there will be a right time and place for their use... and I fear that, like XML, generics (and Aspects, as well) have the potential to become a mess in the hands of the inexperienced programmer, designer or architect.

Update: We just migrated this functionality into our production environment. We, therefore, had to rewrite parts of it to not use generics. The upshot was that the code was just as easy to write and understand without the generics involved. The whole feature was 400 lines of code, and the non-generics version was only 7 lines longer than the original. --JohnPerkins


Can we do equivalent of the following in GJ or JDK1.5?

class D1 {
	public: void toString() {
		cout << "D1";
	}
};

class D2 {
	public: void toString() {
		cout << "D2";
	}
};

template <class T> class D_A : T {
	public: void toString() {
		T::toString();
	}
};

int main() {
	D_A<D1>* da1 = new D_A<D1>();
	D_A<D2>* da2 = new D_A<D2>();

da1->toString(); da2->toString();

delete da1; delete da2;

getchar();
}

If so, then it would solve most of the multiple interface needs in Java.

-- VhIndukumar

As far as I know GJ doesn't allow what you're describing above (mixin) due to the method used to implement Generics in GJ. I'm not sure why exactly it isn't possible. By the way, .NET uses a different generics mechanism that also doesn't allow mixins. --AndrewQueisser


See also: PizzaLanguage, NiceLanguage

EditText of this page (last edited November 2, 2006) or FindPage with title or text search