You could do it by wrapping in a template 
    
    
    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 declaredInScope(inFoo):
        callApiFromFoo()
      elif declaredInScope(inBar):
        callApiFromBar()
      else:
        echo "You can call API only in foo or bar scopes!"
    
    proc foobar =
      foo:
        callApi()
      
      bar:
        callApi()
    
    foobar()
    
    
    Run

Reply via email to