Skip to main content

31  R8 in a package

A package declares its types, generics, methods, traits, and impls at the top level of its R code, as the previous chapters do, and registers them when it loads:

# R/resampler.R
Resampler <- R8::new_enum("Resampler", variants = list(...))

# R/resample.R
resample <- R8::generic("resample", R8::params(x, n_cases))

R8::implement(resample, Resampler) <- function(x, n_cases) {
  ...
}

# R/zzz.R
.onLoad <- function(libname, pkgname) {
  R8::r8register()
}

DESCRIPTION lists R8 in Imports and names the package’s namespace:

Imports: R8
Config/R8/namespace: resampling

The declarations run when the package is installed, and r8register() registers them in each session that loads the package; loading the package again with pkgload::load_all() replaces them. A method of another package’s S3 generic, such as summary(), is registered in that generic’s namespace.

A package exports a type object, such as Resampler, and a generic, such as resample, as it exports a function; the type object is the constructor. A generic or trait declared in one package can be implemented for a type of another. The package that implements it imports it, with importFrom(resampling, resample) in its NAMESPACE, so that R loads the declaring package first. Documentation with roxygen2 writes the package’s NAMESPACE and help pages from roxygen2 blocks.

31.1 Changing a type that other packages use

Do not change an exported type that other packages use without coordinating the change with them. R does not reinstall the packages that use a type when the package that declares it is updated, so a user may have any combination of versions.

An exported type is part of its package’s interface, as an exported function is. A change that looks harmless in one package may break another:

Change of the type Code that builds values Code that reads values Values made before Structs that extend it
descriptions, defaults, the ID or message of a rule works works work built again for a default
a new field of type option() works works r8update() built again
a new required field breaks works refused built again
fields in another order works by name works r8update() built again
a tighter bound, or a new rule breaks for the values outside it works r8update() for the values inside it built again
a field removed or renamed, or another type breaks breaks refused built again
a looser bound, or a rule removed works Rust code of other packages is refused r8update() built again
another unit, or a higher version works Rust code of other packages is refused refused built again

Rust code of another package is compiled against the declaration as it was. R8 compares the two declarations: the package that uses the type warns when it loads, and its methods that read a value of the type raise an error. A change of meaning that the declaration does not state, such as a field that holds another quantity under the same name, type, and unit, is not seen, and takes a higher version. Values made before a change are the subject of the next section. A struct that extends a type of another package is refused at load until its package is built again.

To change a type that other packages use:

  1. Prefer a new field of type option(). For another kind of change, consider a new type with a conversion from the published one, and leave the published one as it is.
  2. Raise the package’s version, and the type’s version when its values take another meaning or its earlier documents do not read: r8from_json() refuses a document of another version. State the change in NEWS.md.
  3. Find the packages that use the type (tools::package_dependencies("pa", reverse = TRUE)), and run their checks against the new version.
  4. Tell their maintainers what changes, and give them time to release. The CRAN Repository Policy asks for at least two weeks.
  5. A package that is built for the new declaration requires it, with a minimum version in its DESCRIPTION:
Imports:
    R8,
    pa (>= 2.0.0)

A package whose Rust crate depends on the crate of pa requires the same version in Cargo.toml, and vendors the crate again. Its Imports state the version whose declarations the crate was compiled against.

31.2 Values made before a change

A value carries the declaration that it was made under, wherever it goes: in a file, to a worker, or through a session in which its type is declared again. A file of January holds doses in milligrams:

Dose <- new_struct("Dose", fields = list(
  drug = type_character(),
  amount = field(type_double(min = 0), unit = "mg")
))
file <- tempfile(fileext = ".rds")
saveRDS(Dose(drug = "gentamicin", amount = 500), file)

In November the package gives the dose per kilogram of body weight:

Dose <- new_struct("Dose", fields = list(
  drug = type_character(),
  amount = field(type_double(min = 0), unit = "mg/kg")
))
dose <- readRDS(file)
dose
<demo::Dose>
  drug: "gentamicin"
  amount: 500
Made under another declaration of `demo::Dose` than the registered one: `amount` was in `mg`, and is in `mg/kg`; r8update() does not convert it: its fields may mean something else.
dose$amount
[1] 500

The value shows what it holds, and is refused wherever it is used: as a field, as an argument, by a method, in an assignment, a comparison, a hash, and a document. No code takes 500 mg for 500 mg/kg:

r8to_json(dose)
Error:
! Cannot write `demo::Dose`: 1 problem
- The value must be a value of `demo::Dose`, found one made under another declaration of `demo::Dose` than the registered one: `amount` was in `mg`, and is in `mg/kg`; r8update() does not convert it: its fields may mean something else. [declaration_changed]

r8update() gives the value under the registered declaration, for a value or for every value of a list, where the fields mean the same: each is declared under the same name, as the same kind of type, in the same unit, and with its factor levels in the same order, and every new field is optional or has a default. The result is validated. A unit that changed is not such a change:

r8update(dose)
Error:
! Cannot update the value: it was made under another declaration of `demo::Dose` than the registered one: `amount` was in `mg`, and is in `mg/kg`; its fields may mean something else [declaration_changed].

A new optional field is:

Dose <- new_struct("Dose", fields = list(
  drug = type_character(),
  amount = field(type_double(min = 0), unit = "mg"),
  route = option(type_character())
))
r8update(dose)
<demo::Dose>
  drug: "gentamicin"
  amount: 500
  route: NULL

A change that has no part in a value, such as a description, a default, or the message of a rule, leaves the values made before as they are. A type whose values Rust holds reads a saved value only under the declaration that it was saved with.

31.3 Values outside the session

A value survives saveRDS() and readRDS(), and travels to parallel workers (mirai, future), R-stored or Rust-stored. A session or worker that has not loaded the defining package reads its fields; one that loads it receives a full value, with dispatch and validation. R loads R8 to read a value, so R8 is installed there. Version 2 of R’s serialization format (saveRDS(version = 2)), which R has not written by default since 3.6.0, holds a list of the fields in place of the value, as dput() does; such a list is refused where the value is expected.

R reads a saved object without a check, and a file that a disk or a transfer damaged may end the session. r8write_rds() writes an RDS file with a checksum, which r8read_rds() compares first; readRDS() reads the file too:

file <- tempfile(fileext = ".rds")
r8write_rds(Dose(drug = "gentamicin", amount = 500), file)
r8read_rds(file)
<demo::Dose>
  drug: "gentamicin"
  amount: 500
  route: NULL
bytes <- readBin(file, "raw", file.size(file))
bytes[60] <- as.raw(0)
writeBin(bytes, file)
r8read_rds(file)
Error:
! `/var/folders/cr/lgjpmnv97lg6qtnjt9kph7qw0000gn/T//Rtmp8oGtUE/file5ca61e4c540d.rds` has no checksum: it is damaged at its end, or r8write_rds() did not write it. Restore it from a copy, or write it again; readRDS() reads a file of saveRDS() without a check.
© 2026 E.D. Gennatas