Hello,
I'm sure this functionality exists, however having a real hard time finding the answer.
How to you query the type of an object?
From what I've been trying, type() only seems to work for primitive types.
I've also tried to find the answer in "Nim in Action".
In my search I also found the module typetraits, however name() doesn't seem to work either.
For example:
import strutils, typetraits, unicode
type
Token = object of RootObj
Integer = ref object of Token
String = ref object of Token
let d = Integer()
let c = String()
echo name(d)
Gives:
main.nim(17, 10) Error: type mismatch: got (Integer)
but expected one of:
proc name(t: typedesc): string
exit status 1
We have "is" and "of" in Nim for static and dynamic type tests.
Note that your custom names "String" and "Integer" may be a bit confusing, as people unfamiliar with Nim may guess that that are basic Nim types.
This code works for me:
import strutils, typetraits, unicode
type
Token = object of RootObj
Integer = ref object of Token
String = ref object of Token
X = ref object of Integer # inheritance
let d = Integer()
let c = String()
#
var t: Token
echo t is Token
echo d is Integer
echo c is String
var x: X
new x
var s = newSeq[Integer]()
s.add(x)
echo s[0] is X # false, because static base type of is is seq[Token]
echo s[0] of X # true, dynamik test
echo name(type(x))
$ ./t
true
true
true
false
true
X
Note that Nim is statically typed, so dynamically runtime type test are generally only used for OOP design with inheritance, which is not used that much in Nim.
You may tell us exactly what you want to archive, then you may get a more concrete answer.
Name is used like this:
import typetraits
let x = 1
echo x.type.name
Notice the type proc that returns the type from a variable.
That was already included in my example, but with () notation:
echo name(type(x))
Seems that import typetraits is not necessary for latest Nim compiler.
Stefan,
"is" is what I was looking for!. My use case is not very complicated as I'm just trying trying to learn by experimenting. The code was just an example.
I have some ideas on a project I want to make using nim, for example a translator that converts a markup to groff code.
The idea being to read the input and convert it to an AST, then traverse the AST, generating groff code based on the node type.
However after experimenting a bit more, I think what I have in mind is best achieved by writing a variety of "generate" methods each accepting a different parameter type, and leveraging dynamic dispatch to let the compiler decide which "generate" proc to use, depending on which node type is passed to it.
This way I could traverse the AST recursively, simply calling the same proc over and over.
for example:
method generate(element: H1) : string method generate(element: H2) : string method generate(element: UL) : string method generate(element: LI) : string method generate(element: P) : string
All this to say, although "is" is what I had in mind when posting the question, I'm realizing I may not need it after all if I leverage methods :)
yc