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 =
|
proc `$`*(rec: RecordClass): string =
|
||||||
$rec.label & "/" & $rec.arity
|
$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 =
|
proc init*(rec: RecordClass; fields: varargs[Preserve, toPreserve]): Preserve =
|
||||||
## Initialize a new record value.
|
## Initialize a new record value.
|
||||||
assert(fields.len == rec.arity)
|
assert(fields.len == rec.arity)
|
||||||
|
|
|
@ -24,10 +24,8 @@ suite "conversions":
|
||||||
a, b: int
|
a, b: int
|
||||||
c: Bar
|
c: Bar
|
||||||
let
|
let
|
||||||
c: Foobar = (a: 1, b: 2, c: Bar(s: "ku",))
|
tup: Foobar = (a: 1, b: 2, c: Bar(s: "ku",))
|
||||||
b = toPreserve(c)
|
prs = toPreserve(tup)
|
||||||
a = preserveTo(b, Foobar)
|
check(prs.kind == pkRecord)
|
||||||
check(a == c)
|
check(preserveTo(prs, Foobar) == tup)
|
||||||
check(b.kind == pkRecord)
|
check(classOf(tup) == classOf(prs))
|
||||||
check(classOf(c) == RecordClass(label: symbol"foo", arity: 3))
|
|
||||||
check(classOf(Foobar) == RecordClass(label: symbol"foo", arity: 3))
|
|
||||||
|
|
Loading…
Reference in New Issue