A generic is a function that dispatches on the R8 type of its first
argument to the method that implement() declares for that type. A
package assigns it to the generic's name, documents it with roxygen2, and
exports it. The generic is a trait with one method named as the generic,
ns::describe, which Rust and the C interface implement as any trait.
Arguments
- name
Character: The generic's name, an R8 name such as
"describe": a letter, then letters, digits, or underscores; or such a name followed by<-for a replacement generic, such as"labels<-". It is the name the generic is assigned to.- params
The generic's parameters, from
params(). The first is the value dispatched on. A generic with"..."lets each method add named arguments after the generic's; a generic without it calls its methods with exactly its arguments, and a call with another argument is an error. Removing"..."from a generic breaks the methods that add arguments.- description
Character: What the generic does.
- dispatch
Optional Character: The arguments the generic dispatches on, by name, at most three and none of them
...; the first argument by default. A method names a type for any of them, by position or by name, and a position it does not name matches any value: withdispatch = c("x", "y"),implement(f, A, B)specializes both,implement(f, A)the first, andimplement(f, y = B)the second. The generic forces exactly these arguments to choose a method. The most specific method in every position applies; two methods each more specific in one position make a call on both ambiguous, anr8_dispatch_errorthat names the method to declare, andr8ambiguities()lists such pairs.- return_type
Optional: The type of the value that every method returns, an
r8_field_type, an R8 type, or"Self", the type of the first dispatched argument. A method may declare a type that fits it (implement<-). A replacement generic returns a value of its argument's type and declares none.- rules
A
rule(), or a list of rules, over the typed parameters, which every call satisfies once each argument satisfies its type, before the method is chosen.- x
A generic of class
r8_generic.- ...
Not used.
Value
generic(): The generic, a function of class r8_generic. print(): x, invisibly.
Details
A call checks its typed arguments before it dispatches: it forces them, in
the order of the parameters, converts them as a constructor does, such as
a whole double for an integer, and raises r8_argument_error with every
problem of every argument. A result that does not satisfy the return type
raises r8_result_error. An argument without a type stays lazy. The checks
belong to the generic's call: r8method() and r8next_impl() call a
method without them.
A replacement generic, generic("labels<-", params(x, value)), is called
as labels(x) <- value, which rebinds x to the method's result; the
original value is unchanged. Its last argument is value, and each
method returns a value of the type of x: for a child type, the child,
such as x after x$label <- value; any other result raises
r8_result_error. Its trait is ns::with_labels, the method's name in the
bindings other than R.
See also
params() declares the parameters; implement<- declares a
method; r8method() returns one.
Examples
op <- options(r8.namespace = "models")
GBM <- new_struct("GBM", fields = list(n_trees = type_integer(min = 1L)))
describe <- generic(
"describe",
params(x, digits = arg_integer(min = 0L, default = 3L)),
return_type = type_character()
)
implement(describe, GBM) <- function(x, digits) {
paste("Gradient boosting,", format(x$n_trees, digits = digits), "trees")
}
describe(GBM(n_trees = 100L))#> [1] "Gradient boosting, 100 trees"
`n_trees<-` <- generic("n_trees<-", params(x, value))
implement(`n_trees<-`, GBM) <- function(x, value) {
x$n_trees <- value
x
}
m <- GBM(n_trees = 100L)
n_trees(m) <- 200L
m$n_trees#> [1] 200options(op)