generic programming - How to get string representation of an expr in Nim template -
is there possibility string representation of expression (or identifier) inside template
? example, having next code:
template `*`*(name: expr) {.immediate.} = var `name`* {.inject.}: string = "" # want print 'name' here, not value backticks
is possible string representation of name
expression inside template?
you can use asttostr
magic system module this:
template foo*(name: untyped) = var `name`* {.inject.}: string = "" # want print 'name' here, not value backticks echo "the variable name ", name.asttostr foo test
the output be:
the variable name test
the use of immediate
pragma discouraged latest version of compiler. see following answer more details: typed vs untyped vs expr vs stmt in templates , macros
Comments
Post a Comment