Skip to contents

A product type: named fields, each with a type and per-field facts, and rules over them. The type object constructs values, Svm(cost = 2), and inherits(x, Svm) tests for a value of the type or of a struct that extends it. Types declared in a package belong to its namespace (the DESCRIPTION field Config/R8/namespace, or the package name); types declared at the console belong to the option r8.namespace, "global" by default.

Type@from(x) converts x with the type's From impls, as r8converter(Type)(x) does.

Usage

new_struct(
  name,
  fields = list(),
  description = "",
  version = 1L,
  rules = list(),
  extends = NULL,
  defaults = list(),
  storage = c("r", "rust")
)

# S3 method for class 'r8_struct_type'
object@name

Arguments

name

The item's name.

fields

Named list: The fields, field(...) calls or bare field types.

description

Character: What the type is.

version

Integer: The type's version, from 1.

rules

A rule(), or a list of rule() calls, over the fields. A child's rules add to the rules it inherits.

extends

Optional R8 struct type: The parent. The child holds the parent's fields, then its own fields, and the parent's rules, then its own rules; its values belong to the parent's class, and impls of the parent apply to them where the child has none.

defaults

Named list: The child's defaults of inherited fields, by field name. NULL removes the default of an option() field.

storage

Character {"r", "rust"}: Where values hold their fields. "rust": a Rust-stored type, whose values the codec in the package's compiled exports builds and holds. Its constructor signals r8_codec_missing_error until the codec is registered, and r8register() checks that the exports carry the codec. A package whose types are declared in Rust generates these declarations with r8document_rust().

object

A struct type object.

Value

The type object, of class r8_struct_type.

The item.

Changing a type that other packages use

An exported type is part of its package's interface. Do not change it without coordinating the change with the packages that use it: R does not reinstall them when the declaring package is updated, and a user may have any combination of versions.

  • A field that is removed or renamed, another type or unit, a loosened bound, a removed rule, and another version stop compiled code of other packages from reading values of the type (r8register(), section "Types that other packages use"). A new required field and a tightened bound or rule break code that constructs values. Every change of a struct's fields or rules requires the structs that extend it to be built again.

  • A new field of type option() is the change that breaks the least. For a change of another kind, consider a new type, with a conversion (r8convert()), and leave the published one as it is.

  • When a published type has to change, raise the package's version, and the type's version when its values take another meaning or its earlier documents do not read; state the change in NEWS, run the checks of the reverse dependencies, and give their maintainers time to release. The CRAN Repository Policy asks for at least two weeks.

  • A value carries the declaration that it was made under. One made before a change, such as a saved one, is refused where it is used until r8update() gives its value under the new declaration, which it does for a new optional field and for another order of fields, and does not for another unit, kind of type, or version, and for a removed field.

  • A package that depends on the new declaration requires it: a minimum version in the DESCRIPTION field Imports, such as Imports: pa (>= 2.0.0), and, for a package with Rust code, the same version of the crate in Cargo.toml, vendored again and rebuilt.

Examples

op <- options(r8.namespace = "demo")
Point <- new_struct("Point", fields = list(x = type_double(), y = type_double()),
  description = "A point.")
Range <- new_struct("Range", fields = list(lo = type_double(), hi = type_double()),
  rules = rule(lo <= hi, message = "lo is at most hi."))
Window <- new_struct("Window", fields = list(label = type_character()),
  extends = Range)
w <- Window(lo = 1, hi = 2, label = "w")
w
#> <demo::Window>
#>   lo: 1
#>   hi: 2
#>   label: "w"
inherits(w, Range)
#> [1] TRUE
options(op)