Traits that R8 declares in its namespace R8, which a type implements
with impl() like any trait:
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", ahost_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, ...), whichprint()shows for a type without aPrintimpl.Debug:str(object, ...).PartialOrd:partial_cmp(x, y), which returns-1L,0L, or1Lasxsorts before, with, or aftery, andNAwhen the two are not ordered; a whole double converts, as in construction, and any other result raisesr8_result_error.<,<=,>, and>=use it, and returnFALSEfor values that are not ordered, as Rust's do;r8partial_cmp()calls it. A result of0Lagrees withr8equal().From(source):from(x), which converts a value of the class or R8 typesourceto the implementing type;r8convert(),Type@from(), andr8converter()use it.
Two are R's host traits, for R's own functions:
Print:print(x, ...), whichprint()calls before it returnsxinvisibly;format()keeps theDisplaytext.
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.50m < Money(cents = 200L)#> [1] TRUEoptions(op)