ArrayFactory SAM type / toArray
Sam Pullara
spullara at gmail.com
Wed Sep 19 14:15:57 PDT 2012
Presumably because you don't know the size of the array until the
lambda is called if this is on Stream.
I think that you should just overload .into with a factory so you
could even pre allocate collections.
Sam
Any errors are bugs in iOS 6
On Sep 19, 2012, at 2:02 PM, Remi Forax <forax at univ-mlv.fr> wrote:
> On 09/19/2012 10:39 PM, Brian Goetz wrote:
>> In looking at the Collection API, there are two forms of toArray
>> method, both of which are unfortunate:
>>
>> Object[] toArray() -- returns an Object[], not a T[]
>>
>> T[] toArray(T[]) -- reflective instantiation
>>
>> Lambdas offer us a way out of this:
>>
>> interface ArrayFactory<T> {
>> T[] make(int n);
>> }
>>
>> interface Collection<T> {
>> T[] toArray(ArrayFactory<T> factory) default {
>> return toArray(factory.make(size());
>> }
>> }
>>
>> The default is imperfect (though no worse than what clients typically
>> do), and concrete implementations of Collection can do better.
>>
>> Given that Stream has a toArray method, my preference would be to expose
>>
>> T[] toArray(ArrayFactory<T>)
>>
>> possibly as the only toArray method.
>>
>> We might be able to extend the constructor reference syntax to arrays:
>> Foo[]::new. If not, n -> new Foo[n] works fine.
>
> Why passing a lambda that will be called in the body of the toArray ?
> It's simpler to directly pass the array.
>
> Rémi
>
>
>
>
>
>
>
More information about the lambda-libs-spec-observers
mailing list