Defines a trait's methods for a type, or for a tuple of types when its methods dispatch on several arguments; a method the impl omits takes the trait's default implementation. Rust's orphan rule applies: an impl outside the trait's namespace names a type of its own namespace, with only types before it. Implementing the same trait for the same types again replaces the impl; an impl that another namespace declares is refused.
Usage
impl(trait, target, methods = list(), tag_value = NULL)Arguments
- trait
A trait object.
- target
What the trait is implemented for: an R8 type object or a class (
host_class()); for a trait whose methods dispatch on several arguments, a list of them, one per dispatched argument the impl specializes, by position or by name, aslist(A, B)orlist(y = B).- methods
Named list: One function per method, or
impl_method()for a method with its own return type.- tag_value
Optional Character: The value of the trait's tag member for this type; defaults to the type's name.
Examples
op <- options(r8.namespace = "demo")
Describe <- new_trait("Describe", methods = list(describe = trait_method(params(x))))
Point <- new_struct("Point", fields = list(x = type_double(), y = type_double()))
impl(Describe, Point, methods = list(describe = function(x) sprintf("(%g, %g)", x$x, x$y)))
describe <- Describe@describe
describe(Point(x = 1, y = 2))#> [1] "(1, 2)"options(op)