|
Re: Have Generics Killed Java?
|
Posted: Jul 22, 2010 9:08 AM
|
|
I would argue, the problem is not Generics, but the lack of tuple unpacking and the library.
Looking at
Map<String,Object> map = getInfo();
Map<String,Object> sortedMap = new TreeMap<String,Object>(map);
there seems to me no compelling reason the EntrySet could not have a "sorted" method, that returns an Iterable<Map.Entry<K,V>>, so it's a library thing we cannot write
for(Map.Entry<String, Object> e : getInfo().entrySet().sorted()) { ...
The next problem is, that for a intuitive usage, we dont want to think of key,value-pairs, but of two separate variables, a key, and a value.
In ruby, this is done via "tuple unpacking". Just try this:
{:one => 1, :two => 2 }.each do |pair| puts pair.inspect end it reveals, that "each" passes a key,value-pair to the closure, just like java's Map.Entry<K,V>. The big difference is "tuple unpacking", (i.e. the fact, that in ruby you can write a, b = 1, 2), which allows to pass the lines "key = pair[0]; value = pair[1]"
Without a nicer library and tuple unpacking, ruby's version would look like this:
map = getMap
treemap = treeMap.new(map)
treemap.each do |pair|
key = pair[0]
value = pair[1]
...
end
which looks in my opinion like the java version, and sucks as much too.
Therefore i think, the generics have nothing to do with it.
|
|