pub enum Val<I = i32, F = f64> where
I: DataType + PrimInt + Signed,
F: DataType + Float, {
Int(I),
Float(F),
Bool(bool),
Error(ExError),
None,
}
Expand description
feature = "value"
-
The value type Val
can contain an integer, float, bool, none, or error.
To use the value type, there are the is a parse function parse_val
.
In the following example, the ternary Python-style a if condition else b
is used.
This is equivalent to if condition {a} else {b}
in Rust or condition ? a : b
in C.
use exmex::{Express, Val};
let expr = exmex::parse_val::<i32, f64>("1.0 if x > y else 73")?;
assert_eq!(expr.eval(&[Val::Float(3.4), Val::Int(3)])?.to_float()?, 1.0);
assert_eq!(expr.eval(&[Val::Int(34), Val::Float(132.0)])?.to_int()?, 73);
Note that the ternary operator is actually implemented as two binary operators called if
and else
.
To this end, we return Val::None
from the if
-operator if and only if the condition is false. On the flipside,
this has strange side effects such as 5 else 3
being a valid expression evaluating to 5.
use exmex::Express;
let expr = exmex::parse_val::<i32, f64>("5 else 3")?;
assert_eq!(expr.eval(&[])?.to_int()?, 5);
We use the variant Error
to report errors, since the trait Try
is not yet stable.
use exmex::Express;
let expr = exmex::parse_val::<i32, f64>("fact(3.5)")?;
let res = expr.eval(&[])?;
assert!(format!("{:?}", res) == "Error(ExError { msg: \"did not expect Float(3.5)\" })");
When converting the value to the expected primitive type with to_int
, to_float
, or to_bool
, the case Val::Error(ExError)
is
converted to ExResult::Err(ExError)
.
assert!(res.to_int().is_err());
Variants
Int(I)
Float(F)
Bool(bool)
Error(ExError)
Since the trait Try
is experimental, we keep track of an error in an additional variant.
None
Sometimes, Val
does not contain a value
Implementations
Trait Implementations
sourceimpl<I: Clone, F: Clone> Clone for Val<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
impl<I: Clone, F: Clone> Clone for Val<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
sourceimpl<I: Debug, F: Debug> Debug for Val<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
impl<I: Debug, F: Debug> Debug for Val<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
sourceimpl<I, F> FromStr for Val<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
<I as FromStr>::Err: Debug,
<F as FromStr>::Err: Debug,
impl<I, F> FromStr for Val<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
<I as FromStr>::Err: Debug,
<F as FromStr>::Err: Debug,
sourceimpl<I, F> MakeOperators<Val<I, F>> for ValOpsFactory<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
<I as FromStr>::Err: Debug,
<F as FromStr>::Err: Debug,
impl<I, F> MakeOperators<Val<I, F>> for ValOpsFactory<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
<I as FromStr>::Err: Debug,
<F as FromStr>::Err: Debug,
sourceimpl<I, F> PartialEq<Val<I, F>> for Val<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
<I as FromStr>::Err: Debug,
<F as FromStr>::Err: Debug,
impl<I, F> PartialEq<Val<I, F>> for Val<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
<I as FromStr>::Err: Debug,
<F as FromStr>::Err: Debug,
sourceimpl<I, F> PartialOrd<Val<I, F>> for Val<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
<I as FromStr>::Err: Debug,
<F as FromStr>::Err: Debug,
impl<I, F> PartialOrd<Val<I, F>> for Val<I, F> where
I: DataType + PrimInt + Signed,
F: DataType + Float,
<I as FromStr>::Err: Debug,
<F as FromStr>::Err: Debug,
sourcefn partial_cmp(&self, other: &Val<I, F>) -> Option<Ordering>
fn partial_cmp(&self, other: &Val<I, F>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
Auto Trait Implementations
impl<I, F> RefUnwindSafe for Val<I, F> where
F: RefUnwindSafe,
I: RefUnwindSafe,
impl<I, F> Send for Val<I, F> where
F: Send,
I: Send,
impl<I, F> Sync for Val<I, F> where
F: Sync,
I: Sync,
impl<I, F> Unpin for Val<I, F> where
F: Unpin,
I: Unpin,
impl<I, F> UnwindSafe for Val<I, F> where
F: UnwindSafe,
I: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more