import typetraits
type Thing = ref object of RootObj
name:string
type Aged = ref object of Thing
age:int
# does type.name only report STATIC declared type
var x = new(Thing)
echo("x's type=",int.name) #this COMPILED
#echo("x's type=",x.type.name) #gives SYNTAX ERROR got (type x.name)
var y = new(Aged)
echo("y's type=",y.type.name)
var z = y #this COMPILED
#var z:Thing = y #gives SYNTAX ERROR got (type z.name)
echo("z's type=",z.type.name)
# hack for runtime (dynamic) type
method typeName(th:Thing):string {.base.} = "Thing"
method typeName(ag:Aged):string = "Aged"
echo("x typeName=",x.typeName())
echo("y typeName=",y.typeName())
echo("z typeName=",z.typeName())
unfortunately what I wanted to try will not compile. If either of the two indicated commands are replaced by the following alternate - I get a syntax error.
Is this a nim compiler bug??? I am using
Nim Compiler Version 0.12.0 (2015-10-27) [Linux: i386]
on a 32bit Linux box.
echo("x's type=",name(x.type))
Don't ask me why it's not equivalent.
type Thing = ref object of RootObj
foo:string
Then it compiles. So the compiler is confused by the fact that there's a field called "name"