pub enum Value {
Nothing,
Bool(bool),
Numb(f64),
Text(Rc<str>),
List(Rc<Vec<Value>>),
Namespace(Scope),
Func {
params: Box<Value>,
body: Box<Node>,
closure: Scope,
},
Builtin(BuiltinFn),
Promise(Promise),
Undefined(Undef),
Tuple(Rc<Vec<Value>>),
Name(Rc<str>),
}Expand description
Any first-class Swan value.
Cloning is cheap: heap-backed variants (Text, List, Tuple, Name)
use Rc, and Scope, Promise and BuiltinFn are handles that share
state when cloned.
The Swan type name of each variant (as returned by the type built-in) is
the variant name, except that Builtin reports Func.
§Examples
use swan_core::value::Value;
let t = Value::tuple(vec![Value::Numb(1.0), Value::Nothing, Value::Numb(2.0)]);
assert_eq!(t.items().count(), 2);
assert!(bool::from(t));Variants§
Nothing
The empty tuple (). Also the result of failed lookups such as out-of-range list indexing.
Bool(bool)
A boolean, TRUE or FALSE.
Numb(f64)
A 64-bit floating-point number.
Text(Rc<str>)
An immutable UTF-8 string.
List(Rc<Vec<Value>>)
An ordered, integer-indexed sequence — [a, b, c].
Namespace(Scope)
A first-class namespace — { x: 1, y: 2 }.
Backed by a Scope. Applying a namespace to a Text key performs an
own-only lookup (parent scopes are not consulted) and yields Nothing
if the key is absent. Equality compares bindings (and parents) by value.
Func
A user-defined function.
Fields
Builtin(BuiltinFn)
A native Rust function callable from Swan.
Promise(Promise)
A promise. It resolves at most once and never rejects; an Undefined result resolves to Nothing.
Undefined(Undef)
The result of an invalid operation, such as "a" - 1.
Swan expressions do not throw exceptions; they return Undefined
values instead. The Undef payload describes what went wrong.
Tuple(Rc<Vec<Value>>)
The Swan product type — a heterogeneous sequence of items produced by
the comma operator a, b, c. A Tuple of zero items is Nothing.
A Tuple of exactly one item normalises to that item.
Construct with Value::tuple so the result is normalized.
Name(Rc<str>)
An identifier in a binding position: the left side of =, : or
->, or the operand of $. It is produced only while evaluating those
positions and has no meaning as a runtime result.
Implementations§
Source§impl Value
impl Value
Sourcepub fn undef_unary(op: &'static str, val: Value, pos: usize) -> Value
pub fn undef_unary(op: &'static str, val: Value, pos: usize) -> Value
Builds Undefined for unary operation op applied to val; pos is the source byte offset.
Source§impl Value
impl Value
Sourcepub fn items(&self) -> ValueIter ⓘ
pub fn items(&self) -> ValueIter ⓘ
Returns an iterator over this value’s items, treating it as a tuple.
Nothingyields no items.Tupleyields its items (recursively flattened,Nothingskipped).- Any other value yields itself as the single item.
Sourcepub fn pair(&self, other: &Value) -> Pairs ⓘ
pub fn pair(&self, other: &Value) -> Pairs ⓘ
Returns an iterator yielding corresponding pairs of items between
self and other, treating each value as a tuple.
If the two values have different lengths, the missing items from the
shorter tuple are replaced with Value::Nothing.
Sourcepub fn normalize(&self) -> Self
pub fn normalize(&self) -> Self
Returns the canonical form of this value.
For a Tuple, nested tuples are flattened and Nothing items are
removed. The result is Nothing if no items remain, the sole item if
one remains, and a Tuple otherwise. Non-tuple values are returned
unchanged (cloned).
Sourcepub fn map(&self, f: impl Fn(Value) -> Value + 'static) -> Value
pub fn map(&self, f: impl Fn(Value) -> Value + 'static) -> Value
Maps a function f: Value -> Value over this Value:
Value::Nothing->Value::NothingValue::Tuple->Value::Tuplecontainingf(v1), f(v2), ..., f(vn)- Any other value ->
f(self)The result of mapping over a tuple is normalized, so items that map toNothingare dropped.
Trait Implementations§
Source§impl From<Value> for bool
Truthiness of a value.
impl From<Value> for bool
Truthiness of a value.
NothingandUndefinedare false.Boolis itself;Numbis false only for0;TextandListare true when non-empty;Namespaceis true when it has at least one binding.FuncandBuiltinare always true.- A
Promiseis true once it is resolved (whatever its value) and false while it is pending. - A
Namehas no boolean meaning and is false. - A
Tupleis true if at least one item is true.
Source§impl PartialEq for Value
Structural equality.
impl PartialEq for Value
Structural equality.
Values of different variants are never equal. Promises are equal only
if they are the same promise (pointer identity). Func, Builtin and
Name are never equal to anything, including themselves. Undefined
values compare by operation and operands, ignoring source position.
Source§impl PartialOrd for Value
Partial ordering between values.
impl PartialOrd for Value
Partial ordering between values.
Only values of the same variant are comparable (Bool, Numb, Text,
List, Tuple); lists and tuples compare lexicographically, with the
shorter sequence ordering first. Nothing equals Nothing and orders
before every other value. Every other pair returns None
(e.g. NaN, mixed types, Func, Builtin, Namespace, Promise, Undefined).