Refactor Tuple and Record to share ValueList

Introduce `ValueList` type alias for `Rc<Vec<Value>>` to reduce
boilerplate and improve readability.
Add `as_slice()` method to `Value` to provide a unified way to access
the underlying values of Tuples and Records.
Update `flatten_value` and `unpack` in `VM` to utilize the new
`as_slice()` method, simplifying logic and improving consistency.
This commit is contained in:
Michael Schimmel
2026-02-21 15:16:04 +01:00
parent 26f0ed9531
commit dc81b7d616
2 changed files with 28 additions and 27 deletions
+9 -24
View File
@@ -601,18 +601,12 @@ impl VM {
}
fn flatten_value(val: Value, into: &mut Vec<Value>) {
match val {
Value::Tuple(values) => {
for item in values.iter() {
Self::flatten_value(item.clone(), into);
}
if let Some(values) = val.as_slice() {
for item in values.iter() {
Self::flatten_value(item.clone(), into);
}
Value::Record(r) => {
for item in r.values.iter() {
Self::flatten_value(item.clone(), into);
}
}
_ => into.push(val),
} else {
into.push(val);
}
}
@@ -685,26 +679,17 @@ impl VM {
Ok(())
}
BoundKind::Tuple { elements } => {
// If the current value at offset is a Tuple (List or Record), we dive into it.
// If the current value at offset is a Tuple or Record, we dive into it.
// Otherwise, we assume the sequence was already flattened (e.g. by Specializer).
match values.get(*offset) {
Some(Value::Tuple(t)) => {
if let Some(val) = values.get(*offset) {
if let Some(sub_values) = val.as_slice() {
*offset += 1;
let mut sub_offset = 0;
for el in elements {
self.unpack(el, t, &mut sub_offset)?;
self.unpack(el, sub_values, &mut sub_offset)?;
}
return Ok(());
}
Some(Value::Record(r)) => {
*offset += 1;
let mut sub_offset = 0;
for el in elements {
self.unpack(el, &r.values, &mut sub_offset)?;
}
return Ok(());
}
_ => {}
}
for el in elements {