import marshal
type Foo = object
a: int
b: string
when isMainModule:
let f = Foo(a: 5, b: "hello")
let a = $$(f)
echo a
let b = a.to[Foo]
echo b
but the compilation fails with
example.nim(11, 12) template/generic instantiation from here
lib/pure/marshal.nim(256, 39) Error: type mismatch: got (T)
but expected one of:
typeinfo.toAny(x: var T)
Looking at the source, it seems that the culprit is the last line in
proc to*[T](data: string): T =
## reads data and transforms it to a ``T``.
var tab = initTable[BiggestInt, pointer]()
loadAny(newStringStream(data), toAny(result), tab)
in particular calling toAny on result. Now, as far as I understand, result is always of mutable, even though not declared explicitly. What is wrong in this? How is marshal supposed to work?