Forum Stats

  • 3,852,844 Users
  • 2,264,142 Discussions
  • 7,905,157 Comments

Discussions

Generic syntax

843793
843793 Member Posts: 41,732 Green Ribbon
edited Aug 18, 2002 8:58PM in Generics
Hey, I just realized what generics are ;)

I think the angle bracket syntax for generics are ugly. Why not have a MagicType class that does not have to be casted? All the data structures can have an optional Class[] parameter that says what classes are allowed.

Example:
GenericList list = new GenericList(new Class[] { String.class });
list.add("test");
String a = list.remove(0);
System.out.println(a);
GenericList's psuedocode would look like this:
class GenericList {
   public GenericList(Class[] types) {
      // save the types in an instance variable.
   }
   public void add(Object a) {
      // check if a instanceof types[x], if not, throw RuntimeException
   }
   public MagicType remove(int index) {
     // MagicType is an Object that automatically casts to something
     return objectAtIndex(index);
   }

Comments

  • brucechapman
    brucechapman Member Posts: 466
    Hey,

    I'm all for re-inventing wheels. without that we wouldn't have pneumatic spoked ones, and my bicycle would be built by a stone mason, and I'd need a bullock to take me anywhere.

    However, we established years ago that round (not square) is the ideal shape. Each re-invention should proceed from current knowledge. Check out http://www.jcp.org/jsr/detail/14.jsp and http://www.jcp.org/aboutJava/communityprocess/review/jsr014/index.html and http://developer.java.sun.com/developer/earlyAccess/adding_generics/ then if you can improve on that, let the expert group know.
This discussion has been closed.