library(S7)
typed <- function(fun, ...) {
..params <- list(...)
..syms <- names(..params)
stopifnot(..syms %in% names(formals(fun)))
# should make this a function
..validator <- S7::new_class("..validator", properties = ..params)
# todo include a return -- something executed on.exit()
..ugh <- substitute(
do.call(
..validator,
as.list(match.call())[intersect(names(as.list(match.call())), ..syms)]
),
environment()
)
body(fun) <- as.call(c(`{`, ..ugh, as.list(body(fun))))
fun
}
foo <- function(a = 1, b = 2) {
return(a + b)
}
foo(a = 1)
#> [1] 3
bar <- typed(
foo,
a = class_integer,
b = class_integer
)
bar(a = 1L)
#> [1] 3
try(bar(a = 1.0))
#> Error : <..validator> object properties are invalid:
#> - @a must be <integer>, not <double>
try(bar(a = Sys.Date()))
#> Error : <..validator> object properties are invalid:
#> - @a must be <integer>, not S3<Date>
Created on 2024-10-04 with reprex v2.1.1
Wait, this could be good:
Created on 2025-03-04 with reprex v2.1.1