Struct extendr_api::wrapper::function::Function
source · pub struct Function { /* private fields */ }
Expand description
Wrapper for creating functions (CLOSSXP).
use extendr_api::prelude::*;
test! {
// Closures are functions.
let expr = R!("function(a = 1, b) {c <- a + b}")?;
let func = expr.as_function().unwrap();
let expected_formals = Pairlist::from_pairs(vec![("a", r!(1.0)), ("b", missing_arg().into())]);
let expected_body = lang!(
"{", lang!("<-", sym!(c), lang!("+", sym!(a), sym!(b))));
assert_eq!(func.formals().unwrap(), expected_formals);
assert_eq!(func.body().unwrap(), expected_body);
assert_eq!(func.environment().unwrap(), global_env());
// Primitives can also be functions.
let expr = R!("`~`")?;
let func = expr.as_function().unwrap();
assert_eq!(func.formals(), None);
assert_eq!(func.body(), None);
assert_eq!(func.environment(), None);
}
Implementations§
source§impl Function
impl Function
sourcepub fn from_parts(
formals: Pairlist,
body: Language,
env: Environment
) -> Result<Self>
pub fn from_parts( formals: Pairlist, body: Language, env: Environment ) -> Result<Self>
Make a function from parts.
use extendr_api::prelude::*;
test! {
let formals = pairlist!(a=NULL);
let body = lang!("+", sym!(a), r!(1)).try_into()?;
let env = global_env();
let f = r!(Function::from_parts(formals, body, env )?);
assert_eq!(f.call(pairlist!(a=1))?, r!(2));
}
sourcepub fn call(&self, args: Pairlist) -> Result<Robj>
pub fn call(&self, args: Pairlist) -> Result<Robj>
Do the equivalent of x(a, b, c)
use extendr_api::prelude::*;
test! {
let function = R!("function(a, b) a + b").unwrap().as_function().unwrap();
assert_eq!(function.call(pairlist!(a=1, b=2)).unwrap(), r!(3));
}
sourcepub fn formals(&self) -> Option<Pairlist>
pub fn formals(&self) -> Option<Pairlist>
Get the formal arguments of the function or None if it is a primitive.
sourcepub fn environment(&self) -> Option<Environment>
pub fn environment(&self) -> Option<Environment>
Get the environment of the function or None if it is a primitive.
Trait Implementations§
source§impl Conversions for Function
impl Conversions for Function
as_*()
source§fn as_language(&self) -> Option<Language>
fn as_language(&self) -> Option<Language>
Convert a language object to a Language wrapper. Read more
source§fn as_pairlist(&self) -> Option<Pairlist>
fn as_pairlist(&self) -> Option<Pairlist>
Convert a pair list object (LISTSXP) to a Pairlist wrapper. Read more
source§fn as_expressions(&self) -> Option<Expressions>
fn as_expressions(&self) -> Option<Expressions>
Convert an expression object (EXPRSXP) to a Expr wrapper. Read more
source§fn as_environment(&self) -> Option<Environment>
fn as_environment(&self) -> Option<Environment>
Convert an environment object (ENVSXP) to a Env wrapper. Read more
source§fn as_function(&self) -> Option<Function>
fn as_function(&self) -> Option<Function>
Convert a function object (CLOSXP) to a Function wrapper. Read more
source§fn as_promise(&self) -> Option<Promise>
fn as_promise(&self) -> Option<Promise>
Get a wrapper for a promise.
source§impl Operators for Function
impl Operators for Function
dollar() etc.
source§fn dollar<T>(&self, symbol: T) -> Result<Robj>where
T: AsRef<str>,
fn dollar<T>(&self, symbol: T) -> Result<Robj>where T: AsRef<str>,
Do the equivalent of x$y Read more
source§fn slice<T>(&self, rhs: T) -> Result<Robj>where
T: Into<Robj>,
fn slice<T>(&self, rhs: T) -> Result<Robj>where T: Into<Robj>,
Do the equivalent of
x[y]
Read moresource§fn index<T>(&self, rhs: T) -> Result<Robj>where
T: Into<Robj>,
fn index<T>(&self, rhs: T) -> Result<Robj>where T: Into<Robj>,
Do the equivalent of
x[[y]]
Read moresource§fn tilde<T>(&self, rhs: T) -> Result<Robj>where
T: Into<Robj>,
fn tilde<T>(&self, rhs: T) -> Result<Robj>where T: Into<Robj>,
Do the equivalent of x ~ y Read more