This function creates a instantiator wrapper for a class by inspecting
the required arguments of its ..new..
and ..init..
methods. It
captures the class name from the provided argument, so it is best
invoked directly, e.g., my_class <- make_instantiator(MY_CLASS)
.
Usage
make_instantiator(class_env, env = parent.frame(), new_defaults = alist())
Arguments
- class_env
Environment. A class environment.
- env
Environment. Environment in which the wrapper should be defined.
- new_defaults
Alist. New defaults to be used in the formal argument of the wrapper. Formal argument not presented in
..new..
or..init..
will be ignored and a warning will be raised. See alsoalist()
.
Details
Care must be taken when either ..new..
or ..init..
includes ...
as a
formal argument, as this may lead to unexpected behavior in argument passing
within the wrapper function. After creating the wrapper, it is recommended to
reorder or modify the formals as needed using formals()
.
Examples
MYCLASS <- new_class(class_name = "MYCLASS")
register_method(MYCLASS, ..init.. = function(name) self$name <- name)
myclass <- make_instantiator(MYCLASS)
myclass
#> function (name, env = new.env(parent = parent.frame()), init_call = sys.call())
#> {
#> MYCLASS$instantiate(name = name, env = env, init_call = init_call)
#> }
#> <environment: 0x10b39d1f8>
myclass("Mike")$name
#> [1] "Mike"
myclass_2 <- make_instantiator(MYCLASS, new_defaults = alist(name = "MIKE"))
myclass_2()$name
#> [1] "MIKE"