Infix operator % for constructing records
This commit is contained in:
parent
24d974b91f
commit
be8ad62f99
|
@ -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)
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue