Skip to content

Document order of execution const_added vs inherited #12759

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

Merged
merged 1 commit into from
Apr 10, 2025

Conversation

fxn
Copy link
Contributor

@fxn fxn commented Feb 16, 2025

I don't have a real use where this matters, but thought perhaps you'd like to document the order of execution here.

If it was preferred to leave this unspecified, then I could redo the patch to say so.

What do you think?

/cc @byroot

@fxn fxn force-pushed the const_added_docs branch 2 times, most recently from 0a09fa1 to 6d23b38 Compare February 16, 2025 19:30
@fxn
Copy link
Contributor Author

fxn commented Feb 16, 2025

Let me add.

When const_added is invoked, the class object is already created and subclassing already happened. You can const_get within const_added and see it.

However, I believe it is expected that the subclass has already a name in inherited. So, in that sense, the current order makes sense, I suppose! 😅

@fxn
Copy link
Contributor Author

fxn commented Feb 16, 2025

Oh, I have a use case!

Since Zeitwerk nowadays sets autoloads for namespaces on const_added, this order guarantees code remains autoloadable in inherited hooks.

@nobu
Copy link
Member

nobu commented Feb 17, 2025

Please file an issue on https://bugs.ruby-lang.org.
We'll discuss it on the next meeting.

@fxn
Copy link
Contributor Author

fxn commented Feb 17, 2025

Thanks @nobu, I have created ruby#21143.

@@ -199,5 +199,25 @@ def self.const_added(name)

ScratchPad.recorded.should == [123, 456]
end

it "for a class defined with the `class` keyword, const_added runs before inherited" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a class not using the class keyword?
Then inherited must run first since the inheritance happens before the assignment (if even assigned at all).
Could you add a spec for it too even though it's kind of obvious?
It also illustrates those 2 cases are inconsistent in the order.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to test with module too for extra coverage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a class not using the class keyword?

Yeah, I was like, that case is obvious because the hooks run in series, there is no ambiguity there. But happy to add a spec for completeness.

I'll update the patch later!

@fxn fxn closed this Mar 20, 2025
@fxn fxn deleted the const_added_docs branch March 20, 2025 22:27
@fxn fxn restored the const_added_docs branch March 21, 2025 07:38
@fxn fxn reopened this Mar 21, 2025
@byroot byroot force-pushed the const_added_docs branch from 9c2accd to 007d9bf Compare April 10, 2025 08:30
@byroot byroot enabled auto-merge (rebase) April 10, 2025 08:30
@byroot byroot merged commit 08ce626 into ruby:master Apr 10, 2025
42 checks passed
@fxn fxn deleted the const_added_docs branch April 20, 2025 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants