Get RecordClass of tuple types
This commit is contained in:
parent
2eda0fc9a7
commit
24d974b91f
|
@ -43,10 +43,14 @@ proc classOf*(val: Preserve): RecordClass =
|
|||
RecordClass(label: val.label, arity: val.arity)
|
||||
|
||||
proc classOf*[T](x: T): RecordClass =
|
||||
## Derive the ``RecordClass`` of ``x``.
|
||||
when not T.hasCustomPragma(record): {.error: "no {.record.} pragma on " & $T.}
|
||||
result.label = preserves.symbol(T.getCustomPragmaVal(record))
|
||||
for k, v in x.fieldPairs: inc(result.arity)
|
||||
|
||||
proc classOf*(T: typedesc[tuple]): RecordClass =
|
||||
## Derive the ``RecordClass`` of ``T``.
|
||||
# TODO: would be nice to get the class of a type without an instance.
|
||||
when not T.hasCustomPragma(record):
|
||||
raise newException(Defect, "{.record: \"…\".} must be present to determine classOf")
|
||||
else:
|
||||
result.label = preserves.symbol(T.getCustomPragmaVal(record))
|
||||
for k, v in x.fieldPairs: inc(result.arity)
|
||||
when not T.hasCustomPragma(record): {.error: "no {.record.} pragma on " & $T.}
|
||||
RecordClass(
|
||||
label: preserves.symbol(T.getCustomPragmaVal(record)),
|
||||
arity: tupleLen(T))
|
||||
|
|
|
@ -5,30 +5,29 @@ import bigints, preserves, preserves/records
|
|||
|
||||
suite "conversions":
|
||||
test "dictionary":
|
||||
type Bar = tuple
|
||||
type Bar = object
|
||||
s: string
|
||||
type Foobar = object
|
||||
type Foobar = tuple
|
||||
a, b: int
|
||||
c: Bar
|
||||
let
|
||||
c = Foobar(a: 1, b: 2, c: (s: "ku",))
|
||||
c: Foobar = (a: 1, b: 2, c: Bar(s: "ku",))
|
||||
b = toPreserve(c)
|
||||
a = preserveTo(b, Foobar)
|
||||
check(a == c)
|
||||
check(b.kind == pkDictionary)
|
||||
expect Defect:
|
||||
checkpoint $classOf(c)
|
||||
|
||||
test "records":
|
||||
type Bar {.record: "bar".} = tuple
|
||||
type Bar {.record: "bar".} = object
|
||||
s: string
|
||||
type Foobar {.record: "foo".} = object
|
||||
type Foobar {.record: "foo".} = tuple
|
||||
a, b: int
|
||||
c: Bar
|
||||
let
|
||||
c = Foobar(a: 1, b: 2, c: (s: "ku",))
|
||||
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))
|
||||
|
|
Loading…
Reference in New Issue