How can I deal with `emit`? When I use `emit` in global scope, it generates 
`emit` statement before normal Nim code.
    
    
    import parseutils
    
    
    template procName*: string =
      when declared(NimMainModule):
        echo "sdf"
      
      when not declaredInScope(internalCProcName):
        var internalCProcName {.exportc:"__the_name_should_not_be_used", 
inject.}: cstring
        {.emit: "__the_name_should_not_be_used = __func__;".}
        var realProcNameButShouldnotBeUsed {.inject.}: string
        discard parseUntil($internalCProcName, realProcNameButShouldnotBeUsed, 
"__")
      realProcNameButShouldnotBeUsed
    
    
    echo procname()
    
    Run

It got crashed. 
[https://play.nim-lang.org/#ix=2rr](https://play.nim-lang.org/#ix=2rr)
    
    
    ccache\@mtest.nim.c:76:33: error: initializer element is not computable at 
load time
    __the_name_should_not_be_used = __func__;
                                   ^~~~~~~~
    ccache\@mtest.nim.c:101:24: error: conflicting types for 
'__the_name_should_not_be_used'
    N_LIB_PRIVATE NCSTRING __the_name_should_not_be_used;
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ccache\@mtest.nim.c:76:1: note: previous definition of 
'__the_name_should_not_be_used' was here
    __the_name_should_not_be_used = __func__;
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Run

It generates C code like:
    
    
    __the_name_should_not_be_used = __func__;
    
    
.........................................................................................
    
    /* section: NIM_merge_VARS */
    N_LIB_PRIVATE NCSTRING __the_name_should_not_be_used;
    N_LIB_PRIVATE NimStringDesc* 
realProcNameButShouldnotBeUsed__Ipp9c7O8sXEIShSL2Z09bqPw;
    
    Run

I need something like this:
    
    
    /* section: NIM_merge_VARS */
    N_LIB_PRIVATE NCSTRING __the_name_should_not_be_used;
    N_LIB_PRIVATE NimStringDesc* 
realProcNameButShouldnotBeUsed__Ipp9c7O8sXEIShSL2Z09bqPw;
    
    
......................................................................................
    
    __the_name_should_not_be_used = __func__;
    
    Run

Examples in 
[https://github.com/nim-lang/Nim/issues/8212](https://github.com/nim-lang/Nim/issues/8212)

Can I achieve that? Any suggestion? Thanks in advance. 

Reply via email to