list literal gotcha and suggestion

Lawrence Kesteloot lk at teamten.com
Sun Sep 27 19:25:07 PDT 2009


I would optimize for consistency with other languages. Eventually we
can stop teaching the array syntax altogether. (I barely know it
myself and the rare times I use it I have to look it up.)

Lawrence


On Sun, Sep 27, 2009 at 7:20 PM, Joshua Bloch <jjb at google.com> wrote:
> Nick,
> Hi! Thanks for the suggestion.  Your point is entirely valid.  That said, I
> first wrote the proposal the way you want it, and this ensued:
>
> ---------- Forwarded message ----------
> From: Tim Peierls <tim at peierls.net>
> Date: Fri, Mar 27, 2009 at 4:45 AM
> Subject: Re: Collection Literals Proposal
> To: Joshua Bloch <jjb at google.com>
> Cc: Neal Gafter <neal at gafter.com>, Java-council <java-council at google.com>,
> Doug Lea <dl at cs.oswego.edu>, "Joseph D. Darcy" <joe.darcy at sun.com>
>
>
> On Fri, Mar 27, 2009 at 2:01 AM, Joshua Bloch <jjb at google.com> wrote:
>
>> Incidentally, I picked curly braces for List because the language already
>> uses them for array initializers, but the more I look at what I proposed the
>> more I think I got it backwards.  It's the opposite of Python, and it just
>> feels wrong.
>>
>
> Yes, you got it backwards. Years of Perl hacking have burned it into me: []
> is empty list/array, {} is empty map/hash/associative array. Also Python,
> Javascript, Ruby, ... Not something you want to fight.
>
> --tim
>
> ***********************************************************************************
>
> So I suppose it's a question of who gets confused:(  Perhaps I could keep
> the syntax as it stands, but encourage IDE maintainers to generate a warning
> if programmers fall into the trap that you describe?
>
>              Josh
>
> On Sat, Sep 26, 2009 at 1:37 PM, Nick Parlante <
> nick.parlante at cs.stanford.edu> wrote:
>
>> Hi there -- up until recently I taught Stanford's Tons Of Java course,
>> CS108, for many years, so I've got a good feel for how new engineers get
>> started with the language and where they have problems. I also run
>> http://javabat.com where you really see what beginner programmers write.
>>
>> Looking at the Project Coin stuff, one corner of the syntax for
>> collection literals jumped out at me as a potential source of problems.
>> Here's the proposed syntax, as lifted from Mark Reinhold's slides.
>>
>> List<Integer> piDigits = [ 3, 1, 4, 1, 5, 9, 2, 6, 5 ];
>> Set<Integer> primes = { 2, 7, 31, 127, 8191, 131071 };
>>
>> My concern is that students will write code like this:
>>
>> List<Integer> piDigits = new ArrayList<>({3, 1, 4, 1});
>>
>> This code looks so reasonable but sadly does something unexpected, since
>> the set literal eliminates the second 1.
>>
>> The students will write it this way, with the { }, since they are
>> accustomed to array initializers that way in Java, and also C/C++.
>>
>> So here's my suggestion:
>>
>> 1. Use { } for list literals, not [ ]. It's most consistent with array
>> literals and avoids the above list/set gotcha.
>>
>> 2. Use [ ] for set literals, or if that causes problems, just don't have
>> a literal for sets. Lists and maps are the most common, so having a
>> syntax for those cases is the most important. Even without a special
>> syntax, a set is easy to express based on a list literal, like
>>   new HashSet<Integer>({1, 2, 3, 1})  //  { } meaning List here
>> It's nice that there's no gotcha with the slight []/{} mis-match going
>> this direction.
>>
>> Cheers,
>>
>> Nick
>>
>>
>>
>>
>>
>>
>
>



More information about the coin-dev mailing list