RFC: Module prepend#29
Open
stakach wants to merge 4 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rendered: https://github.com/stakach/rfcs/blob/rfc/module-prepend/text/0029-module-prepend.md
Summary
Add
prependas a class/module/struct body keyword, analogous toincludeandextend, that inserts a module ahead of the host in its method-resolution order. Methods defined in the prepended module override the host's same-named methods and can delegate to them withsuper— giving Crystal a first-class way to express wrapper / "around" patterns without monkey-patching, proxies, or macro rewriting.Tracer#fooruns first; itssuperreachesService#foo. Multipleprepends stack;prependedjoinsincluded/extendedas a macro hook; cyclic prepends are caught at compile time. The host's ownsuperis unchanged — wrappers see the host, the host doesn't see the wrappers.Motivation
#10504 — Crystal has no first-class wrapping mechanism today. Logging, instrumentation, retries, transactions and other cross-cutting concerns currently require either
previous_def(forces edits to the host), proxies (object identity changes), or macro-rewrites (high-friction, per-wrapper boilerplate).prependcollapses all of those workarounds into a one-line declarative form that composes — and brings Crystal to parity with Ruby'sModule#prepend, easing porting in both directions.Status
Drafted at the user-visible-semantics level. Reference-level explanation covers the lookup-chain change,
supersemantics (the symmetry/asymmetry between wrapper and host), theprependedmacro hook, ancestor exposure to macros, and the cycle / non-module /selferrors. Deferred / unresolved questions: post-definitionModule.prepend, prepend into generic-module instances, and the precise scope of theprependkeyword reservation.