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.
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'stagis 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;Toroption(T)foroption(T); a subtype of the struct forsubtype(); an implementor of the trait fordyn(). A constraint narrows nothing, so a field oftype_double(min = 0)fitstype_double(min = 0)only. Code that receives adyn()value of the trait reads these fields.- object
A trait object.
Examples
op <- options(r8.namespace = "demo")
Fit <- new_trait("Fit", methods = list(fit = trait_method(params(spec, x, y))))
fit <- Fit@fit
options(op)