Oh yes, I could have made that more clear.

Indeed, the default type is untyped. Both for the arguments as well as the 
return type.

And yes, untyped is required to make this work. Essentially untyped is just 
considered as a raw Nim identifier (nnkIdent in macro terms). If you used 
string as an argument, the compiler would understand that as a string at 
runtime. Since the name of the generated proc / etc. has to be known at compile 
time of course, this wouldn't work.

You _can_ (although I don't think with a template) hand a static string, which 
is a string known at compile time and extract an identifier from the string. 
But unless you do more complicated macro things where you might want to 
calculate names of procs you want to generate, this won't be much different 
than just handing a raw identifier.

An example:
    
    
    import macros
    
    macro genproc(prefix: static string): untyped =
      let procName = ident(prefix & "World")
      result = quote do:
        proc `procName`() = echo "Hello world"
    
    genProc("hello") # works, string literal is static
    helloWorld()
    
    const foo = "alsoHello"
    genProc(foo) # a const string is known at CT
    alsoHelloWorld()
    
    # and also
    proc getName(): string =
      result = "finalHello"
    
    var bar {.compileTime.}: string
    static: bar = getName() # some CT computation
    genProc(bar)
    finalHelloWorld()
    
    
    
    Run

Reply via email to