The following program:
import strformat
type
Thingy = ref object of RootObj
f: int
proc newThingy(f: int): Thingy =
new(result)
result.f = f
var t = newThingy(0)
echo $t.type # prints "Thingy"
echo &"t contains {t.f}"
echo &"Type: {t.type.name}"
fails with a compilation error:
/usercode/in.nim(15, 6) template/generic instantiation of `&` from here
/playground/nim/lib/core/macros.nim(533, 7) Error: undeclared field: 'name'
If I replace t.type.name with t.type, I get a somewhat longer compilation error:
/usercode/in.nim(15, 7) Error: type mismatch: got <string, typedesc[Thingy], string>
but expected one of:
proc formatValue(result: var string; value: SomeFloat; specifier: string)
first type mismatch at position: 2
required type for value: SomeFloat
but expression 'typeof(t)' is of type: typedesc[Thingy]
proc formatValue(result: var string; value: string; specifier: string)
first type mismatch at position: 2
required type for value: string
but expression 'typeof(t)' is of type: typedesc[Thingy]
proc formatValue[T: SomeInteger](result: var string; value: T; specifier: string)
first type mismatch at position: 2
required type for value: T: SomeInteger
but expression 'typeof(t)' is of type: typedesc[Thingy]
proc formatValue[T: not SomeInteger](result: var string; value: T;
specifier: string)
first type mismatch at position: 2
required type for value: T: not SomeInteger
but expression 'typeof(t)' is of type: typedesc[Thingy]
template formatValue(result: var string; value: char; specifier: string)
first type mismatch at position: 2
required type for value: char
but expression 'typeof(t)' is of type: typedesc[Thingy]
template formatValue(result: var string; value: cstring; specifier: string)
first type mismatch at position: 2
required type for value: cstring
but expression 'typeof(t)' is of type: typedesc[Thingy]
expression: formatValue(fmtRes_436207701, typeof(t), "")
Can someone please explain why these errors are occurring? From the documentation, x.type is supposed to return a typedesc, and that's supposed to have a name field.
If I comment out the last line, it compiles OK and prints as expected
Thingy
t contains 0