Struct arithmetic_eval::fns::Defer
source · pub struct Defer;
Expand description
Allows to define a value recursively, by referencing a value being created.
It works like this:
- Provide a function as the only argument. The (only) argument of this function is the value being created.
- Do not use the uninitialized value synchronously; only use it in inner function definitions.
- Return the created value from a function.
§Examples
let program = "
recursive_fib = defer(|fib| {
|n| if(n >= 0 && n <= 1, || n, || fib(n - 1) + fib(n - 2))()
});
recursive_fib(10)
";
let program = Untyped::<F32Grammar>::parse_statements(program)?;
let module = ExecutableModule::new("test_defer", &program)?;
let mut env = Environment::new();
env.extend(Comparisons::iter());
env.insert_native_fn("if", fns::If).insert_native_fn("defer", fns::Defer);
assert_eq!(module.with_env(&env)?.run()?, Value::Prim(55.0));
Trait Implementations§
source§impl<T: Clone + 'static> NativeFn<T> for Defer
impl<T: Clone + 'static> NativeFn<T> for Defer
source§fn evaluate(
&self,
args: Vec<SpannedValue<T>>,
ctx: &mut CallContext<'_, T>,
) -> EvalResult<T>
fn evaluate( &self, args: Vec<SpannedValue<T>>, ctx: &mut CallContext<'_, T>, ) -> EvalResult<T>
Executes the function on the specified arguments.
impl Copy for Defer
Auto Trait Implementations§
impl Freeze for Defer
impl RefUnwindSafe for Defer
impl Send for Defer
impl Sync for Defer
impl Unpin for Defer
impl UnwindSafe for Defer
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)