Skip to content
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

Clarification on when to use ggproto_parent() #373

Open
teunbrand opened this issue Jul 19, 2023 · 0 comments
Open

Clarification on when to use ggproto_parent() #373

teunbrand opened this issue Jul 19, 2023 · 0 comments

Comments

@teunbrand
Copy link

In chapter 19.4.5 the following is said about using the ggproto_parent() function:

This pattern is often easier to read than using `ggproto_parent()` and because ggproto objects are stateless it is just as safe.

However, that paragraph doesn't give a lot of guidance about when to use Class$method() and when to use ggproto_parent(Class, self)$method(). I think it would help if it was clearer that the Class$method() approach is safe when no reading or writing to/from self happens (i.e. the Class$method() formals doesn't include self), and less safe otherwise. To give an example a difference occurs:

library(ggplot2)

Drink <- ggproto(
  "Drink",
  property = "cold",
  describe = function(self) {
    print(paste0("This drink is ", self$property))
  }
)

Coffee <- ggproto(
  "Coffee", Drink,
  property = "hot",
  describe_parent = function(self) {
    ggproto_parent(Drink, self)$describe()
  },
  describe_class = function(self) {
    Drink$describe()
  }
)

Coffee$describe_parent()
#> [1] "This drink is hot"
Coffee$describe_class() # Yuck!
#> [1] "This drink is cold"

Created on 2023-07-19 with reprex v2.0.2

In the example Coffee$describe() would of course give the correct answer, but that is besides the point.

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

No branches or pull requests

1 participant