67 points · by alrex021 · 16y ago · 26 comments
List<String> list = ["ham","eggs"];
Is it a linked list, an array list, a vector ... ?This is what the proposal says about that decision:
It is likely to be higher in space and time performance, in that the
compiler can choose efficient implementations for small collections
(such as empty collections and singletons). On the other hand, it is
less flexible, in that you cannot specify the implementation type.
From: http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/0...Personally, my first instinct was to read the syntax as a form of initialisation, not something that would bar me from adding or deleting elements after the set is instantiated.
If I get a variable of type List, I expect to be able to change the list.
The current API already uses this in a bunch of places to return immutable Lists. For example, the lists you get out of Collections.singletonList() or Collections.nCopies() are immutable.
That's a bit broken from an OO perspective (is the interface an interface or not?), but it's a better solution than the const v. non-const container mess that C++/STL has, at least.
The only way of allowing the programmer to do this, that I can think of for now, is by casting perhaps?
List<String> list = (LinkedList)["ham","eggs"];
Just an idea ...
List list = Lists.newArrayList(); for(Integer i = 0; i < 10; i++) { list.add(fn [x] { i + x }); }
Think you just created a list of 10 functions that each add a different number to their argument? If you closed over the i variable, that variable changed and by the end of the loop is 9. All of the functions return their argument plus 9.
In any case, I can't wait to get some form of closures in Java. Best case would be to get a job using a decent programming language, but closures and various data type literals could go a long way to dull the pain until then.
http://stackoverflow.com/questions/271440/c-captured-variabl... http://stackoverflow.com/questions/233673/lexical-closures-i...
I remember bumping into the issue myself in JavaScript, but only once. After I became aware of the concept, it was never a problem again, in any language. Those one-time issues don't worry me. It's the problems that come up over and over again that are worth fixing at the language level.
A good way to explain it is to analogize captured variables to variables used in the body of a loop or conditional. Variables are only ever created when a declaration is evaluated, and a variable has exactly one value at any given time. The obvious alternative, copying the variable when it's closed over, seems more confusing to me. And the current restriction, that captured variables are final, is just going to force hackery.
Ultimately, if Java grunts want closures, they will have to understand variable capturing. It's part of the package.
Admittedly, the Java for loop is a plain C-style for loop, where you actually can modify the loop variable if you wish, so it pretty much has to work that way. But the binding-reuse behavior of, say, Python's for i in range(n) loop makes less sense to me. IIRC, it's also one of the many contentious differences in Common Lisp between the LOOP and ITERATE macro packages.
Java 1.7:
List<String> list = ["item"];
Java 1.2:
List<String> list = new AList<String>() {{ items=["item"]; }};
where: public class AList<T> implements List<T> {
protected T[] items;
// Implementation
}
Set<String> set = {"item"};
Set<String> set = new ASet<String>() {{ items={"item"}; }};
Map<String, Integer> map = {"key" : 1};
Map<String, Integer> map = new AMap<String, Integer>() {{ keys=["key"]; values=[1]; }};
I can post detailed explanation and examples of this technique, which I love to use, in my blog, if somebody will be interested. (English is foreign language to me).It is much better than to use full blown implementation and bunch of setters just to pass around one or two elements.
I often use this technique to pass optional data to/from methods or to program in declarative style in java.
http://code.google.com/p/google-collections/
List<String> list1 = Lists.newArrayList("item"); List<String> list2 = Lists.newArrayList("item1","item2");
Of course, none of these workarounds are close to what Java 7 offers. Just a few more months :-)
See Sandman reply.