macro_rules! decl_func {
( $identifier:ident, $func_type:expr, $predicate:expr, $target:expr ) => { ... };
}Expand description
Given a function name, a FunctionType, a predicate and a target ValueType declares a function. It generates
a wrapper that handles types and unbox parameters.
The predicate expects a Value as parameter and an EvalResult<Value> as output.
§Examples
// Remember to import everything under the `function` module.
use num_parser::{*, function::*};
decl_func!(
// Function name
hypotenuse,
// Function type
FunctionType::Std,
// Predicate
|v: Value| {
// Read the contained data using read_vec_values
read_vec_values!(v, a, b);
// Convert the data to the desired type
let a_as_number = a.as_float()?;
let b_as_number = b.as_float()?;
Ok(
Value::Float(
a_as_number.powi(2) + b_as_number.powi(2)
)
)
},
// Expect a vector as input, as we need multiple parameters.
ValueType::VectorType
);§Generated code
use num_parser::{*, function::*};
fn hypotenuse(arguments: &Vec<Box<Expression>>, context: &Context, depth: u32) -> EvalResult<Value> {
let unboxed = unbox_parameters(arguments, context, depth)?;
type_wrapper(
unboxed,
FunctionType::Std,
ValueType::VectorType,
context,
|v: Value| {
read_vec_values!(v, a, b);
let a_as_number = a.as_float()?;
let b_as_number = b.as_float()?;
Ok(
Value::Float(
a_as_number.powi(2) + b_as_number.powi(2)
)
)
}
)
}