Skip to contents

Traits that R8 declares in its namespace R8, which a type implements with impl() like any trait:

Usage

Display

Debug

Shape

PartialOrd

Print

r8partial_cmp(x, y)

From(source)

Arguments

x, y

Values of one R8 type, for r8partial_cmp().

source

The source converted from: an R8 type object, a class name such as "data.frame" or "numeric", a host_class(), or an S4 class object, as a dispatched position takes it.

Value

A trait object. From() returns one of the internal class r8_from_trait, which holds its type parameter source.

Details

Four are Rust's standard traits, with their Rust meaning:

  • Display: format(x, ...), which print() shows for a type without a Print impl.

  • Debug: str(object, ...).

  • PartialOrd: partial_cmp(x, y), which returns -1L, 0L, or 1L as x sorts before, with, or after y, and NA when the two are not ordered; a whole double converts, as in construction, and any other result raises r8_result_error. <, <=, >, and >= use it, and return FALSE for values that are not ordered, as Rust's do; r8partial_cmp() calls it. A result of 0L agrees with r8equal().

  • From(source): from(x), which converts a value of the class or R8 type source to the implementing type; r8convert(), Type@from(), and r8converter() use it.

Two are R's host traits, for R's own functions:

  • Print: print(x, ...), which print() calls before it returns x invisibly; format() keeps the Display text.

  • Shape: dim(x); nrow() and ncol() follow.

Every type has, without an impl, the behavior of Rust's PartialEq (== and r8equal()), Hash (r8hash(), the hash of the canonical form), Serialize and Deserialize (r8to_json() and r8from_json()), and, for a type whose required fields have defaults, Default (the constructor called with no arguments); these are not traits that a type implements.

Examples

op <- options(r8.namespace = "demo")
Money <- new_struct("Money", fields = list(cents = type_integer()))
impl(Display, Money, methods = list(format = function(x, ...) sprintf("$%.2f", x$cents / 100)))
impl(PartialOrd, Money, methods = list(partial_cmp = function(x, y) sign(x$cents - y$cents)))
impl(From("numeric"), Money, methods = list(
  from = function(x) Money(cents = as.integer(round(x * 100)))
))
m <- r8convert(1.5, Money)
print(m)
#> $1.50
m < Money(cents = 200L)
#> [1] TRUE
options(op)