JEP 186: Collection Literals

Remi Forax forax at univ-mlv.fr
Wed Jan 15 09:14:42 PST 2014


On 01/15/2014 06:04 PM, Per Bothner wrote:
> On 01/15/2014 03:44 AM, Zhong Yu wrote:
>> On Tue, Jan 14, 2014 at 7:17 PM, Per Bothner <per at bothner.com> wrote:
>>> For example, one could define:
>>>
>>> T v = { e1, ..., en}
>>>
>>> as syntactic sugar for:
>>>
>>> TB tmp = T.make_builder();
>>> tmp.add(e1); ..; tmp.add(en);
>>> T v = tmp.build();
>> How is this any better than
>>
>>       T.of(e1, ..., en);
>>
>> ? I don't see how the literal syntax helps code writers or code
>> readers in this case.
> I can think of two reasons:
>
> (1) Target-typing means you don't have to redundantly specify T:
>
> T v = { e1, ..., en};
>
> vs
>
> T v = T.of(e1, ..., en);
>
> (2) Using the T.of form requires allocating an array,
> which is then thrown away.
>
> I don't think (2) is a major justification. (1) may not
> be enough to justify a new language feature by itself,
> though one could argue it's a natural extension of the
> existing syntax for arrays.

I disagree for (1), if you take a look to java.util.EnumSet by example,
you will see that there are multiple overloads of 'of' to avoid to allocate
an array in the common cases.

Rémi




More information about the lambda-dev mailing list