JEP 186: Collection Literals

Per Bothner per at bothner.com
Wed Jan 15 09:04:06 PST 2014


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.
-- 
	--Per Bothner
per at bothner.com   http://per.bothner.com/


More information about the lambda-dev mailing list