Java 7 is in development. I have a dream list of new language features including:-
- operator overloading
- subarray notation
- terse
equalsnotation - terse getter/setter notation
- SuffixTree based regex
I have no idea how to bring these requests to the Java 7 team (some if not all of them, I’m sure they are fully aware of already)… but if anybody knows how I can pester them to consider these, please let me know! Add your own dream updates in the comments, or just feel free to criticise mine
Operator Overloading
Oh please, please, please support a small set of symbols for operator overloading. This debate has been going on since Java began.
Imagine how clean code would be if mathematical sets could be manipulated by code such as
union = a + b;
intersection = a ^ b;
d = union - intersection;
Note these operators don’t affect the contents of a or b, but produce new objects. Operator overloading could also be used so that a + b adds b to a in the same way as a.addAll(b) works.
If the [index] notation could be overloaded, then imagine how much more terse your List code would be. This may cause some backwards compatibility problems with situations where people have used the + operator in a String concatenation context… but why not then allow overloading of only a few operators such as ++, --, and ** which are currently invalid syntax between Objects.
Subarray notation
Currently, the only way to get a sub-array is to use Arrays.subList (or roll your own). This is not only very verbose, but results in copies of primitive values. For mathematical code (especially when working with BLAS or LAPACK) this is disastrous for performance.
I suggest a notation for obtaining a reference to a subarray which indexes the same primitives and Objects as the parent array with no copying involved. A clean notation could be array[from : to]. This would be completely backwards compatible as the : symbol is currently not valid in this position.
Terse notation for a standardised equals
Comparing objects is somewhat awkward. See Effective Java for an excellent discussion on why this is so. The conclusion is that one must generate boilerplate similar to the following when the Object only has 2 internal state variables! (leftBank and torch):-
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ToyStoryState other = (ToyStoryState) obj;
if (leftBank == null) {
if (other.leftBank != null)
return false;
} else if (!leftBank.equals(other.leftBank))
return false;
if (torch == null) {
if (other.torch != null)
return false;
} else if (!torch.equals(other.torch))
return false;
return true;
}
I envisage a shorthand notation such as
equals {
getClass(), // or the keyword instanceof if it is preferred
leftBank,
torch
}
And if people want to create anything non-standard, then let them create the boilerplate. Given that code blocks cannot be named equals, this should be backwards compatible. (Disclaimer: don’t forget to override hashCode, which could also have a similar syntax). Dan has also suggested that annotations on fields may be a more Java alternative. Any update to the current ugliness would be great!
Getter/Setter variables shorthand
I recently learnt Ruby and it has a beautiful little shorthand for defining variables and their associated getter/setter methods. There is no reason why Java couldn’t adopt a similar thing… and imagine how much more terse it would make JavaBeans! For example, the following
private T blah;
/**
* @returns blah
*/
public T getBlah(){
return blah;
}
/**
* @param blah
*/
public void setBlah(T blah){
this.blah = blah;
}
could be summed up as
/** docs and annotations for blah go here */
getset T blah;
Indexing of String for fast repeated regex searching
This isn’t a language request, but for an additional class in the Pattern, Matcher suite.
I recently posted on the SUN Java forums requesting if anybody knew of an implementation of a Suffix Tree based regex engine. This alternative approach to matching regular expressions against Strings requires indexing the String (expensive), but leads to really fast searches.
Such an approach is appropriate to the case where a String is going to be searched many times. Consider the case of searching all the files in a folder. If the files don’t change but there are many searches performed… then it would make sense to create an index to improve search response time. If the indexes were Serializable it would allow for cross-session searching (instant Desktop-search backend!).
UPDATE: I’d also love to see boilerplate reduced for delegates… such as suggested by Blinkley
Alex Miler wrote:
October 16th, 2007 at 9:36 pm