-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
fix(set): map uniqueness #114
base: main
Are you sure you want to change the base?
Conversation
allows the configuration of the collection instance when mapping over values so implementation specific behaviours are retained BREAKING CHANGE: return types have changed
I'm not sure |
I see the BC concern but I also feel like when I'm manipulating a set I want it to behave like a set. Maybe this can be merged into a v3 branch but I think this is an important change. |
Laravel's collections have a mapInto function accepting the collection class name as a second parameter. If the maintainer is open to something like that maybe it could be a good compromise for your use case. |
I think the best approach would be to override the That said, /**
* @extends \Ramsey\Collection\AbstractSet<Foo>
*/
class MySet extends \Ramsey\Collection\AbstractSet
{
public function getType(): string
{
return MyType::class;
}
/**
* @param callable(MyType): TCallbackReturn $callback A callable to apply to each
* item of the collection.
*
* @return MySet
*
* @template TCallbackReturn as MyType
*/
public function map(callable $callback): \Ramsey\Collection\CollectionInterface
{
return new self(array_map($callback, $this->data));
}
} |
I see your point about not introducing a I don't think this change would take away any flexibility of the library we would simply ensure the correct behavior for any implementation of a collection and it still allows users to extend any collection they want to add their own behavior. |
Is the correct behavior for the map function to always return the same type of collection? |
I would argue so yes, when I create a new |
Description
When mapping over a set it is possible to introduce duplicate values which is undesirable.
Motivation and context
This change is so that the behavior of the implementation is respected such as in this case the duplicate values in a
Set
.How has this been tested?
Tests have been added for the
Set
andCollection
implementations that a instance of themselves is returned and that when mapping over an existingSet
not duplicate values can be introduced.Types of changes
The actual return types as declared have not changed but in the case of the
Set
a newSet
is now returned instead of aCollection
as well as the new mapping behavior for theSet
that does now not allow duplicate values anymore.PR checklist