Indexing access syntax for Lists and Maps

Eamonn McManus Eamonn.McManus at Sun.COM
Wed Jul 22 02:40:21 PDT 2009


Reinier Zwitserloot <reinier at zwitserloot.com> writes:
 > 2. Mirroring List's "T get(int)" is just fine, but "boolean set(int, T)"
 > is problematic because of that 'boolean' return value. What should this:
 >
 >  Object x = someArrayList[0] = "Hello";
 >  System.out.println(x);
 >
 > print? People will expect 'Hello'. But if we just 'desugar' "list[idx] =
 > foo" to "list.set(idx, foo)", then this will print "true", which is an
 > instant java puzzler.

In addition to the options you mention later in your message, it seems to me
that a reasonable option would be to say that the value of

someArrayList[0] = "Hello"

is the value assigned.  The fact that list.set returns something else doesn't
have to be relevant.  The JLS currently says that "the result of the assignment
expression is the value of the variable after assignment has occurred", and it
is obvious how this should be extended for lists and maps.

Éamonn McManus · JMX Spec Lead · http://weblogs.java.net/blog/emcmanus/




More information about the coin-dev mailing list