Skip to content

[6.0] Remove mutating methods from lazy collection#29671

Merged
taylorotwell merged 2 commits intolaravel:6.0from
JosephSilber:lazy-collection-remove-mutating-methods
Aug 21, 2019
Merged

[6.0] Remove mutating methods from lazy collection#29671
taylorotwell merged 2 commits intolaravel:6.0from
JosephSilber:lazy-collection-remove-mutating-methods

Conversation

@JosephSilber
Copy link
Contributor

@JosephSilber JosephSilber commented Aug 21, 2019

The removed methods are:

  • add($item)
  • forget($keys)
  • pop()
  • prepend($value, $key = null)
  • push($value)
  • put($key, $value)
  • shift()
  • splice($offset, $length = null, $replacement = [])
  • transform(callable $callback)

I originally added them for feature-parity with the regular collection class, but after giving it some more thought I realized that it doesn't really make sense.

  1. It's very easy to accidentally enumerate values twice, or accidentally enumerate the whole collection.

  2. After removing mutation, I was able to simplify the code quite a bit.


Alternative

Some of these methods could theoretically be implemented without mutation, by just returning a new collection.

However, I believe that would lead to a lot of confusion, since the regular collection class does get mutated with these methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants