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

Don't add braces to function bodies specifically for S4 generics #1141

Open
MichaelChirico opened this issue Aug 5, 2023 · 1 comment
Open

Comments

@MichaelChirico
Copy link
Contributor

Per Advanced R:

It is bad practice to use {} in the generic as it triggers a special case that is more expensive, and generally best avoided.

# Don't do this!
 setGeneric("myGeneric", function(x) {
 standardGeneric("myGeneric")
})

https://github.com/hadley/adv-r/blob/dc49c3872c3530ac08716fd4f4c235b01266a4ce/S4.Rmd#L345-L352

Companion issue for {lintr}: r-lib/lintr#1968

@lorenzwalthert
Copy link
Collaborator

This is a bit more complex with current code since the rule is implemented to check if there is a function token and If yes, add the braces, without knowing about the fact that we are inside setGeneric. Can you identify we are inside setGeneric if standardGeneric is in the function body or is that not true? If it is mostly true, we could avoid adding the curly braces based on a condition where stadnardGeneric() is called inside the function. That would still add braces to a situation like this:

 setGeneric("myGeneric", function(x) {
  othergeneric("myGeneric")
})

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

2 participants