loophp / collection

A (memory) friendly, easy, lazy and modular collection class.
https://loophp-collection.rtfd.io/
MIT License
722 stars 35 forks source link

More specific Chunkable::chunk() return type #350

Open rela589n opened 3 days ago

rela589n commented 3 days ago

Currently Chunkable interface looks like this:

/**
 * @template TKey
 * @template T
 */
interface Chunkable
{
    /**
     * Chunk a collection of items into chunks of items of a given size.
     *
     * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#chunk
     *
     * @return Collection<int, list<T>>
     */
    public function chunk(int ...$sizes): Collection;
}

As you can see, this operation returns Collection<int, list<T>>, which is fair.

Yet, since after applying chunk(), each produced chunk can will never be empty list.

Therefore, maybe it would make sense to specify Collection<int, non-empty-list<T>> as a return type, since this is more specific.

drupol commented 3 days ago

Hello,

Yes! Nice catch.

Please submit a PR to fix this and I'll merge it.