Struct arithmetic_eval::fns::If
source · pub struct If;
Expand description
if
function that eagerly evaluates “if” / “else” terms.
§Type
(using arithmetic-typing
notation)
(Bool, 'T, 'T) -> 'T
§Examples
let program = "x = 3; if(x == 2, -1, x + 1)";
let program = Untyped::<F32Grammar>::parse_statements(program)?;
let module = ExecutableModule::new("test_if", &program)?;
let mut env = Environment::new();
env.insert_native_fn("if", fns::If);
assert_eq!(module.with_env(&env)?.run()?, Value::Prim(4.0));
You can also use the lazy evaluation by returning a function and evaluating it afterwards:
let program = "x = 3; if(x == 2, || -1, || x + 1)()";
let program = Untyped::<F32Grammar>::parse_statements(program)?;
let module = ExecutableModule::new("test_if", &program)?;
let mut env = Environment::new();
env.insert_native_fn("if", fns::If);
assert_eq!(module.with_env(&env)?.run()?, Value::Prim(4.0));
Trait Implementations§
source§impl<T> NativeFn<T> for If
impl<T> NativeFn<T> for If
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 If
Auto Trait Implementations§
impl Freeze for If
impl RefUnwindSafe for If
impl Send for If
impl Sync for If
impl Unpin for If
impl UnwindSafe for If
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
)