pub enum Value {
Float(Float),
Integer(Integer),
Complex(Complex),
Boolean(bool),
Unit,
List(Rc<RefCell<Vec<Value>>>),
Range(Box<Value>, RangeKind, Box<Value>),
Function(Function),
}Expand description
Represents any value that can be stored in a variable.
Variants§
Float(Float)
A floating-point value.
Integer(Integer)
An integer value.
Complex(Complex)
A complex number value.
Boolean(bool)
A boolean.
Unit
The unit type, analogous to () in Rust.
List(Rc<RefCell<Vec<Value>>>)
A list of values.
In cas-rs, a list is a reference to a vector of values. This is done to allow efficient
cloning of lists, as well as mutation of lists in-place. References are passed around
by default, which can result in somewhat confusing behavior, for example:
a = [1, 2, 3]
b = a
b[0] = 4
print(a) // prints [4, 2, 3]TODO: In the future, a clone method may be added to cas-rs to allow the user to
explicitly clone the list instead of copying the reference.
Range(Box<Value>, RangeKind, Box<Value>)
A range representing a sequence of values, either half-open or closed.
Function(Function)
A function.
Functions are treated as values just like any other value in cas-rs; they can be stored
in variables, passed as arguments to other functions, and returned from functions.
Implementations§
Source§impl Value
impl Value
Sourcepub fn coerce_float(self) -> Self
pub fn coerce_float(self) -> Self
Consumes and attempts to coerce the value to a real number. Note that this coercion can
be lossy if converting an arbitrary-precision integer to a fixed-width float. To preserve
precision, see Value::coerce_integer and Value::coerce_number.
This conversion only occurs if one of the following is true:
- The value is an integer.
- The value is a complex number with a zero imaginary part.
This is useful for when evaluation of an expression results in a Value::Complex with a
zero value for the imaginary part. Using a complex number for certain operators, such as
the bitwise operators, will result in an error, so we will need to coerce those values to
Value::Float instead.
Sourcepub fn coerce_integer(self) -> Self
pub fn coerce_integer(self) -> Self
Consumes and attempts to coerce the value to an integer. This coercion is lossless.
This conversion only occurs if one of the following is true:
- The value is a float with a zero fractional part.
- The value is a complex number with a zero imaginary part, and a real part with a zero fractional part.
Sourcepub fn coerce_number(self) -> Self
pub fn coerce_number(self) -> Self
Consumes and attempts to coerce the value to a real number or an integer, preferring integers if possible. This coercion is lossless.
This conversion follows these rules:
- If the value is an integer, it is returned as-is.
- If the value is a float with a zero fractional part, it is converted to an integer. Otherwise, it is returned as-is.
- If the value is a complex number with a zero imaginary part, either an integer or float is returned if the real part is an integer or float, respectively. Otherwise, it is returned as-is.
Sourcepub fn coerce_complex(self) -> Self
pub fn coerce_complex(self) -> Self
Consumes and attempts to coerce the value to a complex number. This coercion is lossless.
Sourcepub fn into_degrees(self) -> Self
pub fn into_degrees(self) -> Self
Converts this value from radians to degrees. If it is a real number, it is converted as usual. If it is a complex number, the real and imaginary parts are converted separately.
Sourcepub fn into_radians(self) -> Self
pub fn into_radians(self) -> Self
Converts this value from degrees to radians. If it is a real number, it is converted as usual. If it is a complex number, the real and imaginary parts are converted separately.
Sourcepub fn is_float(&self) -> bool
pub fn is_float(&self) -> bool
Returns true if this value is a real number, or can be coerced to one.
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Returns true if this value is an integer, or can be coerced to one.
Sourcepub fn is_complex(&self) -> bool
pub fn is_complex(&self) -> bool
Returns true if this value is a complex number, or can be coerced to one.
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Returns true if this value is a boolean.
Sourcepub fn is_truthy(&self) -> bool
pub fn is_truthy(&self) -> bool
Returns true if this value is truthy.
For each type, the following values are considered “truthy”:
Float: any value except0.0andNaNInteger: any value except0Complex: any value except0.0 + 0.0iandNaN + NaNiBool:trueUnit: never true; always falseList: lists with at least one element; element(s) does not have to be truthyRange: ranges with at least one element; element(s) does not have to be truthyFunction: always true
Sourcepub fn fmt(&self, options: FormatOptions) -> ValueFormatter<'_>
pub fn fmt(&self, options: FormatOptions) -> ValueFormatter<'_>
Returns a formatter for the value with the given options.