Repository navigation
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.
|
Thanks for preparing a PR! |
|
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. |
Per review in tidyverse#6854: reading fields through self is fine, assigning to them is what should be discouraged. Restore the Adder/Doubler example without the in-place update, let Doubler override x so the example shows that self is still the child inside ggproto_parent(), and replace the 'stateless classes' paragraph with one sentence on read vs write.
ggplot2's own methods assign to self fields on per-plot objects, so use the wording from the review instead of a flat rule. Also fix the NEWS entry, which read as if methods were required to read fields.
ggproto() example from writing to fields
|
Sorry for the long wait on this one. Reworked it based on your explanation. The Adder/Doubler example is back, but I removed the paragraph about stateless classes and added just one sentence under "Working with ggproto classes": reading fields through If you'd rather keep |
Closes #6582.
The
?ggprotoexample assigned to a field inside a method (self$x <- self$x + n), which is the pattern extension authors should avoid. This keeps the Adder/Doubler example, butadd()now only readsself$x.Doubleroverridesx, so the example also shows thatselfis still the child when the parent's method is called throughggproto_parent()(Doubler$add(10)gives 25).The "Working with ggproto classes" section gets one extra sentence: reading fields through
selfis fine, assigning to them is discouraged.