Skip to contents

A trait declares methods that types implement with impl(). A method is a generic that dispatches on the type of its first argument, reached as Trait@method; a package binds and exports it under its name, such as fit <- Fit@fit. A method named as an S3 generic of the base packages or of the package's imports, such as summary or format, becomes an S3 method of that generic, and its signature begins with the generic's arguments.

Usage

new_trait(
  name,
  methods = list(),
  description = "",
  tag = NULL,
  required_fields = list()
)

# S3 method for class 'r8_trait'
object@name

Arguments

name

Character: The trait name.

methods

Named list: The methods, trait_method() calls.

description

Character: What the trait is.

tag

Optional Character: The member of a dyn() value's JSON form that holds the implementor's type, by its tag value; without it, the member "$type" holds the qualified type name. An enum's tag is another member, which holds a variant's name.

required_fields

Named list: The fields every implementor has, each a field type, an R8 type object, or field(type, description = ). An implementor is a struct whose field of each name has a type that fits the required one: the same type; T or option(T) for option(T); a subtype of the struct for subtype(); an implementor of the trait for dyn(). A constraint narrows nothing, so a field of type_double(min = 0) fits type_double(min = 0) only. Code that receives a dyn() value of the trait reads these fields.

object

A trait object.

Value

The trait object, of class r8_trait.

Examples

op <- options(r8.namespace = "demo")
Fit <- new_trait("Fit", methods = list(fit = trait_method(params(spec, x, y))))
fit <- Fit@fit
options(op)