3 ms·
"Why do we have a special syntax for arrays in the language while collections are implemented in on top of the language? And isn't a bit irritating to convert t
by qw 15y ago
"Why do we have a special syntax for arrays in the language while collections are implemented in on top of the language? And isn't a bit irritating to convert them from one to another all the time?"
Perhaps it's just me, but I do not convert collections to arrays "all the time". I can't remember the last time I did so. It seems that the author has some bad experiences with certain libraries, but he shouldn't assume that it is part of the standard java experience.
I also don't see why this:
for(int idx = 0; idx < names.length; ++idx)
System.out.println(names[idx] + ": " + ages[idx]);
}
is "surprisingly tough to implement cleanly" compared to:
names zip ages foreach {p => println(p._1 + ": " + p._2)}
The second syntax may be clear to Scala programmers, but I think the Java version can be understood by most programmers
- timrod 15y agoI'd write the scala example as names zip ages foreach { (name, age) => println(name + ": " + age) } which, imho, is a little easier on the eyes. [edit: fix syntax]
- jules 15y agoHow does Scala figure out the precedence on that? And in general what does `word1 word2 ... wordN` mean?
- mark242 15y agoThe devil's advocate argument is that it isn't readily apparent that you're using the ages array until you look deep inside the loop. With the scala code, especially with the functional syntax, you know what elements you're working with right up front.