Skip to main content

generate_s3_class

Function generate_s3_class 

Source
pub fn generate_s3_class(parsed_impl: &ParsedImpl) -> ClassWrapper
Expand description

Generates the R wrapper of an S3-style class, in the parts the wrapper writer merges per type (super::ClassWrapper, #1776).

Produces the following R code:

  • Constructor: new_<class>(...) function that calls the Rust constructor and wraps the result with structure(.val, class = "<class>"); none for a class without a constructor (see no_constructor_head)
  • S3 generics: for each instance method, a UseMethod() generic is created (unless overriding an existing generic via #[miniextendr(generic = "...")])
  • S3 methods: <generic>.<class> functions dispatching to the Rust .Call() wrapper, with the ExternalPtr extracted from x
  • Static methods: regular functions named <class>_<method>(...)
  • Class environment: ClassName <- new.env(parent = emptyenv()) for Class$new() syntax and trait namespaces, when the impl has a constructor or the class is named as its Rust type (#1918)

Custom double-dispatch patterns (e.g., vec_ptype2.a.b) are supported via #[miniextendr(generic = "...", class = "...")] attributes.

The constructor and the field methods are the head; the S3 methods and the static methods are the members; the class environment is the tail.