JDK-4984992 : Alpha Feedback: Provide alias statement
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.5
  • CPU: sparc
  • Submitted: 2004-01-28
  • Updated: 2005-01-21
  • Resolved: 2005-01-21
Related Reports
Duplicate :  
Description
One issue with generic which show pretty much immediatly when you start
using them, is that it would be immensely practical to have some kind of
alias statement, like C/C++ typedef.

Consider this code:

public class Example1 {
    private List<Pair<String,String>> internal;

    public Example1() {
      internal = new LinkedList<Pair<String,String>>();
    }

    public Collection<Pair<String,String>> getInternal() {
      return new HashSet<Pair<String,String>>(internal);
    }
}

This is both painful to read, and painful to write. Compare with this:

public class Example2 {
    public alias StringPair Pair<String,String>;

    private List<StringPair> internal;

    public Example2() {
      internal = new LinkedList<StringPair>();
    }

    public Collection<StringPair> getInternal() {
      return new HashSet<StringPair>(internal);
    }
}

As I see it, this would greatly reduce typing, and increase readability.
The feature is not hard to implement, since it only concerns compile time
transformation. The only issue would be that of a new key word, but this
alternate syntax could get away from that too:

public StringPair = Pair<String,String>;