Infix operator % for constructing records

This commit is contained in:
Emery Hemingway 2021-07-07 12:50:57 +02:00
parent 24d974b91f
commit be8ad62f99
2 changed files with 14 additions and 7 deletions

View File

@ -24,6 +24,15 @@ type RecordClass* = object
proc `$`*(rec: RecordClass): string =
$rec.label & "/" & $rec.arity
proc `%`*(rec: RecordClass; field: Preserve): Preserve =
## Initialize a simple record value.
assert(rec.arity == 1)
Preserve(kind: pkRecord, record: @[field, rec.label])
proc `%`*[T](rec: RecordClass; field: T): Preserve =
## Initialize a simple record value.
rec % toPreserve(field)
proc init*(rec: RecordClass; fields: varargs[Preserve, toPreserve]): Preserve =
## Initialize a new record value.
assert(fields.len == rec.arity)

View File

@ -24,10 +24,8 @@ suite "conversions":
a, b: int
c: Bar
let
c: Foobar = (a: 1, b: 2, c: Bar(s: "ku",))
b = toPreserve(c)
a = preserveTo(b, Foobar)
check(a == c)
check(b.kind == pkRecord)
check(classOf(c) == RecordClass(label: symbol"foo", arity: 3))
check(classOf(Foobar) == RecordClass(label: symbol"foo", arity: 3))
tup: Foobar = (a: 1, b: 2, c: Bar(s: "ku",))
prs = toPreserve(tup)
check(prs.kind == pkRecord)
check(preserveTo(prs, Foobar) == tup)
check(classOf(tup) == classOf(prs))