Skip to main content

6  Extending a struct

new_struct(extends = Parent) declares a child: a struct that holds its parent’s fields and rules, then its own. It serves a port from a class system built on inheritance, such as S4, where several classes repeat the fields of a common parent. A closed set of alternatives is an enum (Enums), and an open set of types with one behavior is a trait (Traits and impls).

6.1 A child

Interval holds two ends and a rule between them. Window extends it with a label:

Window <- new_struct(
  "Window",
  description = "A labeled interval.",
  fields = list(label = type_character()),
  extends = Interval
)
Window
<r8 struct demo::Window v1 extends demo::Interval>
  A labeled interval.
  lo: double
  hi: double
  label: character
  rule ORDER: lo <= hi

The constructor takes the parent’s fields first, then the child’s:

w <- Window(lo = 1, hi = 3, label = "first")
w
<demo::Window>
  lo: 1
  hi: 3
  label: "first"

The parent’s rules apply to the child:

Window(lo = 3, hi = 1, label = "inverted")
Error:
! Invalid `demo::Window`: 1 problem
- Rule ORDER: lo is at most hi. [rule_violation]
  lo <= hi; found lo = 3, hi = 1

defaults = gives an inherited field a default of the child’s; a field’s type, unit, and rules stay as the parent declares them, and a rule of the child narrows what the parent allows:

Proportion <- new_struct(
  "Proportion",
  description = "An interval of proportions.",
  extends = Interval,
  defaults = list(lo = 0, hi = 1),
  rules = rule(lo >= 0 && hi <= 1, message = "The ends are from 0 to 1.", id = "FROM_0_TO_1")
)
Proportion(lo = 0.2)
<demo::Proportion>
  lo: 0.2
  hi: 1

6.2 Classes and fields

A child’s value belongs to the class of each ancestor after its own, and r8ancestors() lists them:

class(w)
[1] "demo::Window"   "demo::Interval" "r8_value"      
r8ancestors(Window)
[1] "demo::Interval"
inherits(w, Interval)
[1] TRUE

A field typed as a struct holds a value of that struct only, not of a child. A field that holds a struct or any struct that extends it is subtype(Interval), which includes Interval itself; its JSON names the value’s type in "$type". dyn() takes traits only, as in Rust:

Track <- new_struct("Track", fields = list(span = subtype(Interval)))
cat(r8to_json(Track(span = w)))
{"$type":"demo::Track","$types":{"demo::Track":1,"demo::Window":1},"$declarations":{"demo::Track":"sha256:99c49a48ef7794c6000c4fb7b2fbecc19f9bb93e374eb40dcf3dc90e8dd29a9d","demo::Window":"sha256:864427d4fb64cf1da0aa2cead69266ea3c0455298d8d80623cc5bf78ea282efc"},"span":{"lo":1,"hi":3,"label":"first","$type":"demo::Window"}}

6.3 Methods

A method of the parent runs on the child’s value itself, so a generic called inside it dispatches on the child:

describe <- generic("describe", params(x))

implement(describe, Interval) <- function(x) {
  paste0("[", x$lo, ", ", x$hi, "]")
}

describe(w)
[1] "[1, 3]"

A method of the child takes precedence, and r8next_impl() calls the method it takes precedence over:

implement(describe, Window) <- function(x) {
  paste(x$label, r8next_impl())
}

describe(w)
[1] "first [1, 3]"

For a trait, the impl is the unit of precedence: the nearest type with an impl of the trait provides every method, and a method its impl omits takes the trait’s default.

r8next_impl() passes the current values of the method’s arguments, so a method may change its argument before it continues, as with NextMethod().

6.4 Replacement methods

A replacement method returns a value of its argument’s type. A parent’s replacement method on a child’s value returns the child, as $<- does:

`lo<-` <- generic("lo<-", params(x, value))

implement(`lo<-`, Interval) <- function(x, value) {
  x$lo <- value
  x
}

lo(w) <- 0
w
<demo::Window>
  lo: 0
  hi: 3
  label: "first"

A method that builds a new value of the parent returns the wrong type for a child, which is an error:

implement(`lo<-`, Interval) <- function(x, value) {
  Interval(lo = value, hi = x$hi)
}
lo(w) <- 0.5
Error:
! The replacement generic `demo::lo<-`, from the method for `demo::Interval`, returned a value of `demo::Interval` for a value of `demo::Window`; a replacement method returns a value of its argument's type, such as x after `x$field <- value`, or declare the method for `demo::Window` [replacement_type].

6.5 Conversion

A child’s value converts with the conversion from its own type, or else from its nearest ancestor’s, which receives the child’s value unchanged (Conversion):

Span <- new_struct("Span", fields = list(width = type_double(min = 0)))

as_span <- r8converter(Span)

implement(as_span, Interval) <- function(x) {
  Span(width = x$hi - x$lo)
}

as_span(w)
<demo::Span>
  width: 3

6.6 The parent’s value

r8upcast(x, Parent) returns the value of an ancestor that a child’s value holds: the inherited fields, with the ancestor’s type. r8convert(x, Parent) returns the same value:

r8upcast(w, Interval)
<demo::Interval>
  lo: 0
  hi: 3

6.7 A parent declared again

A child holds a copy of its parent’s declaration. Declaring the parent again with other fields or rules leaves the child stale: it constructs no new value until it is declared again, and the declaration warns:

Interval <- new_struct(
  "Interval",
  fields = list(
    lo = type_double(),
    hi = type_double(),
    closed = field(type_logical(), default = TRUE)
  ),
  rules = rule(lo <= hi, message = "lo is at most hi.", id = "ORDER")
)
Warning: The declaration of `demo::Interval` leaves `demo::Window`,
`demo::Proportion` stale: each extends an earlier declaration. Declare them
again, parent before child.
Window(lo = 1, hi = 3, label = "second")
Error:
! Invalid `demo::Window`: 1 problem
- The value: `demo::Window` was declared against an earlier declaration of `demo::Interval`; declare `demo::Window` again, or reload or reinstall the package that declares it. [type_stale]

In a package, the child of another package’s struct is checked when the package loads; a package built against an earlier declaration of the parent asks to be reinstalled. A package changes a struct that others extend in coordination with them (Changing a type that other packages use).

© 2026 E.D. Gennatas