Skip to content

Instantly share code, notes, and snippets.

@wch
Created June 6, 2023 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wch/2fcdd4c2554477e43c6e909cd682e889 to your computer and use it in GitHub Desktop.
Save wch/2fcdd4c2554477e43c6e909cd682e889 to your computer and use it in GitHub Desktop.
R6 detect if a method was overridden
library(R6)
A <- R6Class(
"A",
public = list(
f = function() {
cat("Called A$f()\n")
if (
is.null(self$.__enclos_env__$super) ||
identical(self$.__enclos_env__$super$g, self$g)
) {
cat("Method A$g was NOT overridden\n")
} else {
cat("Method A$g was overridden\n")
}
},
g = function() {
cat("Called A$g()")
}
)
)
B <- R6Class(
"B",
inherit = A,
public = list(
g = function() {
cat("Called B$g()")
}
)
)
C <- R6Class(
"C",
inherit = A
)
a <- A$new()
a$f()
#> Called A$f()
#> Method A$g was NOT overridden
b <- B$new()
b$f()
#> Called A$f()
#> Method A$g was overridden
c <- C$new()
c$f()
#> Called A$f()
#> Method A$g was NOT overridden
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment