Add toRecord sugar

This commit is contained in:
Emery Hemingway 2023-12-29 18:55:02 +02:00
parent 97ab7ce070
commit 25d42f9498
2 changed files with 9 additions and 0 deletions

View File

@ -207,6 +207,11 @@ proc initRecord*(label: string; args: varargs[Value]): Value {.inline.} =
## Create a Preserves record value.
initRecord(toSymbol(label), args)
proc toRecord*(items: varargs[Value, toPreserves]): Value =
assert items.len > 0
result = initRecord(items[0], items.len.pred)
for i in 0..<items.high: result.record[i] = items[succ i]
proc initSequence*(len: Natural = 0): Value =
## Create a Preserves sequence value.
Value(kind: pkSequence, sequence: newSeq[Value](len))

View File

@ -96,3 +96,7 @@ suite "toPreserve":
test s: check($p == s)
check false.toPreserves, "#f"
check [0, 1, 2, 3].toPreserves, "[0 1 2 3]"
test "toRecord":
let r = toRecord(Symbol"foo", "üks", "kaks", "kolm", {4..7})
check $r == """<foo "üks" "kaks" "kolm" #{4 5 6 7}>"""