Skip to content

Instantly share code, notes, and snippets.

@jrosell
Created October 8, 2025 10:26
Show Gist options
  • Select an option

  • Save jrosell/9b8eb056c1ef1e3097770d71e8b2c18c to your computer and use it in GitHub Desktop.

Select an option

Save jrosell/9b8eb056c1ef1e3097770d71e8b2c18c to your computer and use it in GitHub Desktop.
library(S7)
ScalarDouble <- new_class("ScalarDouble",
properties = list(value = class_double),
constructor = function(value) {
stopifnot(is.double(value), length(value) == 1)
new_object(ScalarDouble, value = value)
}
)
add <- new_generic("add", c("x", "y"))
method(add, list(ScalarDouble, ScalarDouble)) <- function(x, y) {
ScalarDouble(x@value + y@value)
}
a <- ScalarDouble(3.14)
b <- ScalarDouble(2.1)
add(a, b)
# <ScalarDouble> 5.24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment