Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get default value if target element not found #205

Open
spam312sn opened this issue Jul 7, 2019 · 6 comments
Open

Get default value if target element not found #205

spam312sn opened this issue Jul 7, 2019 · 6 comments

Comments

@spam312sn
Copy link

Update get method with one more additional method parameter which will be used as default value if target element couldn't be found.

Example: Symfony ParameterBag

@someniatko
Copy link
Contributor

personally i think $collection->get($id) ?? $default is more expressive, than $collection->get($id, $default)

@malarzm
Copy link
Member

malarzm commented Nov 23, 2019

The $collection->get($id) ?? $default approach won't work if you need to store null in the collection however I'm not sure a lot of people are storing null in the collections.

@SenseException
Copy link
Member

A change of the get() method API would break packages where collection is a dependency and still uses the variant without a default value argument. Projects are implementing the Collection interface and this would also affect Doctrine projects like ORM and the PersistentCollection. At the current time Packagist lists 628 packages that are dependents.

The influence and circumstances of such a change on maintenance compared to its benefit doesn't pay off IMHO.

@alcaeus
Copy link
Member

alcaeus commented Nov 28, 2019

While the BC break is definitely to be avoided, we'll have to think about how to solve this in the future: there were numerous feature requests that would've been nice to implement but were skipped because of the BC implications.

@jwillp
Copy link

jwillp commented Jun 2, 2020

Since the get method returns null in case the value is not found (which serves as some kind of a default), wouldn't the following signature get($key, $default = null) prevent BC?

@someniatko
Copy link
Contributor

@jwillp No, it will not. BC does not only concern interface usage, but also interface implementation.
Changing method signature would mean each CollectionInterface implementor would need to support the second parameter.

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

No branches or pull requests

6 participants