Skip to Main Content

Java APIs

Generic Covariant Java Syntax Only Discussion

843793Jul 25 2002 — edited Sep 4 2002
I have been looking at the 1.2 prototype and comments about it.
Much of the syntax seems unclear and ends up in discussions such as it works for me, generally followed by not if you pass in such and such.
Some of the syntax I saw in some old specs doesn?t seem to be getting any attention at all and so I am unsure if it is still included or not.
The biggest problem is some people (understandably) find much of the proposed syntax hard to understand and just glaze over it, without a complete understanding of what it is trying to do.
I want to discuss the syntax without getting into any details about how java achieves this (code bloat, extra runtime checks etc), in order to understand what should be legal syntax and what the results should be (without having to test it and find out).
Note I am all for both Generic and Covariant Java, so the following assumes we want to do both. Covariant Java means that Object foo() could be overwritten by String foo(), currently Java doesn't allow this (except for with arrays), and so other syntax rules of Java would also need to be changed accordingly.
So in order for Java not to make the same mistakes as many other languages here is my 2 cents worth.

Syntax Examples:

class SampleClass1<TypeA extends Number>
TypeA can be any numerical primitive type (not boolean or char), or any class that extends Number. (Current proposal allows primitive use to be defined in the same way as Object use?)

class SampleClass2<TypeB extends Button implements Comparable, Serializable>
TypeB can be any class that extends Button and implements both Comparable and Serializable. Hence methods from any of these 3 things can be used on TypeB. (Current proposal allows both extends and implements?)

class SampleChildClass3<TypeC, TypeD> extends SampleParentClass3<TypeD>
TypeC can be any class, however TypeD may be bounded by the SampleParentClass3. SampleParentClass3 may have restricted TypeD to extend something. (Current proposal stops us from bounding TypeD any further when it is used by the parent class?)

new SampleClass1();
All the methods using TypeA will take and return Number instead. (Current proposal allows us to create a new instance of Syntax Example 1 without having to explicitly state the extending Type?)

new SampleClass1<double>();
All the methods using TypeA will take and return double instead. (Current proposal allows primitives?)

static TypeC sampleMethod6(TypeD aValue) {?}
TypeC and TypeD are determined when the class is loaded. This method may be found in the class from Syntax Example 3. (Current proposal allows us to define static methods without any generic typing before the static keyword?)

int i = SampleChildClass3<int, String>.sampleMethod6("hello");
The class explicitly determines the parameter types, not implicitly by using the parameters. (Current proposal no longer allows implicit typing as in perl and other such languages?)

More advanced (Covariant) Syntax Examples:

SampleParentClass3<String> a = new SampleChildClass3<int, String>;
SampleChildClass3<int, String> b = (SampleChildClass3<int, String>)a; //legal
SampleChildClass3<long, String> c = (SampleChildClass3<long, String>)a; //legal (due to covariance)
SampleChildClass3<byte, String> d = (SampleChildClass3<byte, String>)a; //runtime error
SampleChildClass3<int, Object> e = (SampleChildClass3<int, Object>)a; // legal (due to covariance)
SampleChildClass3<int, Button> f = (SampleChildClass3<int, Button>)a; //compile error
(Current proposal is consistent with this?)

SampleChildClass3<int, String>.class.getMethod("sampleMethod6", new Class[] {String.class});
Class.forName("SampleParentClass3", new Class[] {String.class});
(Current proposal supports reflection, inner classes etc.?)

(new Collection<Object>).getClass().equals(new Collection().getClass()) // true
(new Collection<String>).getClass().equals(new Collection().getClass()) // false
(Current proposal follows normal Java rules for instanceof and getClass etc.?)

Answers / Comments please?
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 2 2002
Added on Jul 25 2002