list literal gotcha and suggestion

Reinier Zwitserloot reinier at zwitserloot.com
Tue Oct 6 12:22:04 PDT 2009


What _should_ it print? Nothing - it was a rhetorical question to show  
that the concept is a bad idea. The compiler should refuse to compile  
it because its ambiguous.

I was the one who suggested that you must assign the literals to a  
variable. I immediately added that I thought this was a horrible plan.  
If you're going to assign to a variable, then there's not much point  
in creating the literals in the first place - if you have a variable  
reference, then writing a bunch of add/put calls is easy. The lack of  
list/map literals is so annoying precisely BECAUSE you usually need to  
go through declaring a variable name for it, even if your list/map is  
only going to be used in one place. Remember that this is all being  
considered just to allow set literals. We've clearly gone mad.

This discussion is starting to move towards the need to cast this  
literal to make the target typing work. Which it really is - there's  
just no way short of making massive changes to java that 1 type can be  
autoboxed to both List<List<Object>> and Map<Integer, String> - this  
hypothetical initializer type would need to have variable length  
generics in order to make it work: It would have to somehow store the  
notion that this is a list of lists, where each inner list matches the  
type tuple {Integer, String}. Is that really going to be the  
implementation? Seems more likely at this point that the compiler will  
first look at what type is actually required, and only then to start  
checking the structure of the initializer literals to attempt to  
finagle some sort of type out of it. Which is target typing. Which has  
been shot down before, and with good reason.

The suggestion was made in order to simplify syntax. Evidently, Paul  
and possibly Neal seem to think that "(List<>){{1, "One"}, {2,  
"Two"}})" is simpler than "[[1, "One"], [2, "Two"]]".

Is that really what you guys think?

  --Reinier Zwitserloot



On 2009/06/10, at 19:22, Paul Benedict wrote:

> It's a good question. Reinier, what do you think it ought to print?
>
> I thought someone once suggested maybe literals should always be
> assigned a variable. The suggestion is interesting. In terms of
> actually using the language feature, inlining values into methods may
> not be a very valuable language feature. I see the inlining to be used
> more for reference data held in storage.
>
> Paul
>
> On Tue, Oct 6, 2009 at 11:45 AM, Reinier Zwitserloot
> <reinier at zwitserloot.com> wrote:
>> Nobody has yet answered my original question:
>> What does:
>> System.out.println({{1, "One"}, {2, "Two"}});
>> print? Looking at visual basic for syntax advice is not a good idea.
>>
>>  --Reinier Zwitserloot
>>
>>
>> On 2009/06/10, at 18:28, Paul Benedict wrote:
>>




More information about the coin-dev mailing list