Ah, you probably want `declared` instead of `declaredInScope`. The latter only 
works for the immediately enclosing scope and not the ones above it. The 
documentation may not be very clear on that 
[https://nim-lang.org/docs/system.html#declared%2Cuntyped](https://nim-lang.org/docs/system.html#declared%2Cuntyped).
 
    
    
    import macros
    
    template foo(body: untyped) =
      block:
        let inFoo {. inject, used .} = true
        body
    
    template bar(body: untyped) =
      block:
        let inBar {. inject, used .} = true
        body
    
    proc callApiFromFoo =
      echo "Calling API with foo scope"
    
    proc callApiFromBar =
      echo "Calling API with bar scope"
    
    template callApi =
      when declared(inFoo):
        callApiFromFoo()
      elif declared(inBar):
        callApiFromBar()
      else:
        echo "You can call API only in foo or bar scopes!"
    
    proc foobar =
      foo:
        block:
          callApi()
      
      bar:
        block:
          callApi()
    
    foobar()
    
    
    Run

Reply via email to