This (shortened) example from "Nim Tutorial" seems to work:
proc echoHW() =
echo "Hello world"
template joinSymbols(a, b: untyped): untyped =
`a b`()
joinSymbols(echo, HW)
But my code doesn't:
type
VolatilePtr*[T] = distinct ptr T
template declVolatile*(T: typedesc, name: untyped): void =
var `volatile name value` {.global,volatile.}: T
let `name` {.global.}: VolatilePtr[T] = cast[VolatilePtr[T]](addr `volatile name value`)
declVolatile(int, threadCount)
when isMainModule:
echo(threadCount)
This result in those identifiers:
NI volatile volatilenamevalue_l29bFN9bK8y2ZuBqw7Gl4Shg;
NI* name_4LlT1AEFwYS4u0oi4cs1Ig;
when I was expecting "volatilethreadCountvalue..." and "threadCount..."
Also, I'd like to use _ as separators (volatile_ name _value) but apparently it's not allowed. Why?
This works
type
VolatilePtr*[T] = distinct ptr T
template declVolatile*(T: typedesc, name: untyped) {.dirty.} =
var `volatile name value` {.global,volatile.}: T
let `name` {.global.}: VolatilePtr[T] = cast[VolatilePtr[T]](addr `volatile name value`)
declVolatile(int, threadCount)
when isMainModule:
echo(threadCount)
Also, I'd like to use _ as separators (volatile name _value) but apparently it's not allowed. Why?
Never considered that. To influence name mangling, you should use an extern: "volatile_$1_value" pragma anyway.