docs: replace stateful ggproto example with stateless one#6854
Open
CuiweiG wants to merge 1 commit intotidyverse:mainfrom
Open
docs: replace stateful ggproto example with stateless one#6854CuiweiG wants to merge 1 commit intotidyverse:mainfrom
CuiweiG wants to merge 1 commit intotidyverse:mainfrom
Conversation
The prior @examples for ggproto() used stateful classes (Adder$x), a pattern discouraged for extensions since most ggplot2 layer classes are stateless. The new Math/Mather example covers construction, method dispatch and ggproto_parent() without `self$*` access, and the Working-with-classes section adds a paragraph on the stateless norm. Closes tidyverse#6582.
Collaborator
|
Thanks for preparing a PR! |
Author
|
Thanks for the clarification — the write-vs-read distinction wasn't clear to me from the issue alone. Will revise the example to keep the field-based inheritance pattern but drop the mutation. Back to you shortly. |
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.
Closes #6582.
The current
?ggprotoexample demonstrates a stateful class (Adder$x) that, per the issue, "advertises a property of ggproto classes that should be used sparingly and ideally not at all if you're developing extensions."The replacement builds a stateless
Mathclass with two static methods plus aMatherchild overriding one method viaggproto_parent(). Construction, method dispatch, and parent-method invocation are all still covered; noself$*field access appears anywhere. The child method carriesselfin its signature solely to thread identity intoggproto_parent(Math, self).The "Working with ggproto classes" section gains a short paragraph stating that most ggplot2 layer classes (Geoms, Stats, Scales) are stateless, and encouraging extension authors to follow that convention.
Local verification: all example code paths run under
devtools::load_all()with the expected values.