Skip to content

Call Set#add from initialize on subclasses #13518

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tenderlove
Copy link
Member

@tenderlove tenderlove commented Jun 4, 2025

Set#new used to call add on items passed to initialize. This adds backwards compatibility with subclasses of Set

[Bug #21396]

@tenderlove tenderlove force-pushed the set-subclass branch 2 times, most recently from 4ae09eb to 95d8a16 Compare June 4, 2025 19:33
@tenderlove tenderlove requested a review from jeremyevans June 4, 2025 19:40
Set#new used to call add on items passed to initialize.  This adds
backwards compatibility with subclasses of Set

[Bug #21396]
@jeremyevans
Copy link
Contributor

I'm against this for reasons I described in the Redmine ticket. If we want to be backwards compatible, we shouldn't do it piecemeal, we need to do a complete audit of all Set methods and in every case where stdlib Set is calling a Set method, we need to change core Set to call the method instead of directly calling the underlying C function. That's likely to introduce race conditions that we'll have to deal with, and handling those safely will make the code even slower.

@tenderlove
Copy link
Member Author

@jeremyevans I understand. It's kind of a bummer though because it's breaking our existing code. It would be nice if there was some escape hatch, but I'm not sure what that would be.

@ioquatix
Copy link
Member

ioquatix commented Jun 5, 2025

I think it's better to use composition rather than sub-classing if possible in scenarios like this. There are other code paths like IO where sub-classes of IO can override e.g. read and write in Ruby and it's a nightmare to maintain.

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.

3 participants