Skip to contents

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:

Usage

implement(..., return_type = NULL) <- value

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) or implement(combine, y = B); a converter from r8converter() takes an R8 type object or a class name.

return_type

Optional: The type of the value this method returns, an r8_field_type or an R8 type, which fits the return type that the generic declares, as impl_method() states.

value

Function: The method.

Value

The generic, unchanged.

Details

  • A generic from generic(), or a trait method such as Fit@fit: an impl of its trait.

  • format, str, and dim: an impl of the standard trait Display, Debug, or Shape; r8partial_cmp: of PartialOrd, which <, <=, >, and >= use.

  • print: an impl of the standard trait Print; print() calls the function and returns the value invisibly, and format() keeps its own text. The declaration states this in a message.

  • Another S3 generic of base R or of an imported package, such as summary or predict: an S3 method for the qualified class of Type.

  • 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 raise r8_declaration_error. For r8convert and r8converter, the error names the converter's form. A converter from r8converter(Target) is the generic of the conversions to Target: implement(as_target, Source) and implement(as_target, "character") declare the conversion from an R8 type or a class, an impl of From(source) for Target, 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.

See also

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"
#> [1] 10
options(op)