Could you explain how/why it works?

I have reproduced that pattern in my project and it works **sometimes but not 
always** , and I don't understand why.

When it does not work, I have changed the `callApi` template to be like: 
    
    
    template callApi =
      when declaredInScope(inFoo):
        callApiFromFoo()
      elif declaredInScope(inBar):
        callApiFromBar()
      else:
        echo "inFoo scope? = ", declaredInScope(inFoo)
        echo "inBar scope? = ", decalredInScope(inBar)
        error("You can call API only in foo or bar scopes!")
    
    
    Run

And in the `foobar` proc (a macro in my case), I dump the whole AST tree. I can 
see that `inFoo` or `inBar` are correctly defined and deeper in the tree I find
    
    
          LetSection
            IdentDefs
              PragmaExpr
                Ident "inFoo"
                Pragma
                  Ident "inject"
                  Ident "used"
              Sym "Bool"
              Call
                           ...
                              Command
                                Ident "echo"
                                Call
                                  Ident "callApi"
    
    
    Run

(in that specific case, `callApi` returns a `string`)

But the call `callApi` goes in the `else:` branch and I see 
    
    
    echo ["inFoo scope? = ", "false"]
    echo ["inBar scope? = ", "false"]
    error("You can call API only in foo or bar scopes!",
          nil)
    
    
    Run

The case where it fails is more complex than this example. It is like 
`declaredInScope` does not return the correct result in that particular 
situation. I've tried to look at the code for `declaredInScope` but that's a 
[magic 
function](https://github.com/nim-lang/Nim/blob/version-1-2/lib/system.nim#L163) 
and I can't find it to understand how it works.

In case it could be useful to find the source of the problem, compilation fails 
with the error message (which is true because `callApi` expects to return a 
`string` but that's the error case...) 
    
    
    Error: expression has no type:
    echo ["inFoo scope? = ", "false"]
    echo ["inBar scope? = ", "false"]
    error("You can call API only in foo or bar scopes!",
          nil)
    
    
    Run

Reply via email to