Closed jonthegeek closed 1 year ago
(ok, not per se, 'cuz you can't inherit from it... but guidance for how to dispatch for it would be helpful!)
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.
I could have sworn that didn't work! I wonder what I did. Thanks for the tip! 🙃
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!
Ah that I can fix.
For method dispatch,
class_missing
doesn't catchNULL
(which it shouldn't, really). This works, but it doesn't feel quite right:class_null <- S7::new_S3_class("NULL")
. Shouldclass_null
be in the base types?