implement(generic, Type) <- function(x) ... declares the method that
generic calls for values of Type, and returns generic unchanged.
The generic is written as its name. The function has exactly the generic's
arguments, or, for a generic with ..., the generic's arguments first,
... included, then any named arguments of its own. By the generic:
Arguments
- ...
The generic, written as its name and without an argument name, then the types of the method: one R8 type object, or for a generic that dispatches on several arguments, one per dispatched argument it specializes, matched by position or by name, as
implement(combine, A, B)orimplement(combine, y = B); a converter fromr8converter()takes an R8 type object or a class name.- return_type
Optional: The type of the value this method returns, an
r8_field_typeor an R8 type, which fits the return type that the generic declares, asimpl_method()states.- value
Function: The method.
Details
A generic from
generic(), or a trait method such asFit@fit: an impl of its trait.format,str, anddim: an impl of the standard traitDisplay,Debug, orShape;r8partial_cmp: ofPartialOrd, which<,<=,>, and>=use.print: an impl of the standard traitPrint;print()calls the function and returns the value invisibly, andformat()keeps its own text. The declaration states this in a message.Another S3 generic of base R or of an imported package, such as
summaryorpredict: an S3 method for the qualified class ofType.The functions through which R8 reads, changes, and compares values (
$,[[,@,as.list,$<-,[[<-,[<-,names<-,length<-,dim<-,levels<-,==,!=), the comparison and other operators, S4 generics, and functions that are not generics raiser8_declaration_error. Forr8convertandr8converter, the error names the converter's form. A converter fromr8converter(Target)is the generic of the conversions toTarget:implement(as_target, Source)andimplement(as_target, "character")declare the conversion from an R8 type or a class, an impl ofFrom(source)forTarget, and the method takes exactly(x).
An impl is declared in the namespace of its generic or of its type.
In a package, r8rd() documents the method in the generic's Rd file.
Examples
op <- options(r8.namespace = "models")
Resampler <- new_struct(
"Resampler",
fields = list(n_resamples = type_integer(min = 1L))
)
implement(format, Resampler) <- function(x, ...) {
paste("Resampler,", x$n_resamples, "resamples")
}
implement(summary, Resampler) <- function(object, ...) {
object$n_resamples
}
r <- Resampler(n_resamples = 10L)
format(r)#> [1] "Resampler, 10 resamples"summary(r)#> [1] 10options(op)