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

class_null #337

Closed
jonthegeek opened this issue Sep 8, 2023 · 5 comments · Fixed by #340
Closed

class_null #337

jonthegeek opened this issue Sep 8, 2023 · 5 comments · Fixed by #340

Comments

@jonthegeek
Copy link
Contributor

For method dispatch, class_missing doesn't catch NULL (which it shouldn't, really). This works, but it doesn't feel quite right: class_null <- S7::new_S3_class("NULL"). Should class_null be in the base types?

@jonthegeek
Copy link
Contributor Author

(ok, not per se, 'cuz you can't inherit from it... but guidance for how to dispatch for it would be helpful!)

@hadley
Copy link
Member

hadley commented Sep 8, 2023

You can just use a literal NULL:

library(S7)

g <- new_generic("g", "x")
method(g, NULL) <- function(x) "hello"
g(NULL)
#> [1] "hello"

Created on 2023-09-08 with reprex v2.0.2

This presumably should be documented somewhere.

@jonthegeek
Copy link
Contributor Author

I could have sworn that didn't work! I wonder what I did. Thanks for the tip! 🙃

@jonthegeek
Copy link
Contributor Author

Aha! This is (equivalent to) what I tried that failed:

library(S7)

g <- new_generic("g", "x")
method(g, class_missing | NULL) <- function(x) "hello"
#> Error in class_missing | NULL: operations are possible only for numeric, logical or complex types
g(NULL)
#> Error: Can't find method for `g(<NULL>)`.

Created on 2023-09-08 with reprex v2.0.2

vs

library(S7)

g <- new_generic("g", "x")
method(g, class_missing | new_S3_class("NULL")) <- function(x) "hello"
g(NULL)
#> [1] "hello"

Created on 2023-09-08 with reprex v2.0.2

I'm going with the separate method for NULL, but at least now I can see what tricked me!

@hadley
Copy link
Member

hadley commented Sep 9, 2023

Ah that I can fix.

hadley added a commit that referenced this issue Sep 9, 2023
hadley added a commit that referenced this issue Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants