If you don't like reading long post, just read question #4 that synthetize the 
previous questions...

**1\. What does it mean for a template /macro to have ``typed`` and ``untyped`` 
parameters at the same time?**

Can the compiler anticipate the semantic analysis phase for some arguments 
while not considering others?

For instance, in the following template, `typ` would be checked to be a 
`string` while the compiler does not care about the validity of `code`...
    
    
    template dsl(typ: static[string]; code: untyped) =
      ...
    
    
    Run

**2\. Is it possible to have default parameter values in template?**

The preceding example becomes:
    
    
    template dsl(typ: static[string] = "quick"; code: untyped) =
      ...
    
    
    Run

I've tried it but I can't have both blocks below working:
    
    
    dsl "fast":
      # Use fast algorithm
      echo "fast"
    
    dsl:
      # Use quick algorithm
      echo "quick"
    
    
    Run

**3\. Identifier construction in template**

Now I want to call different procedures depending on `typ` value. According to 
the [indentifier 
construction](https://nim-lang.org/docs/manual.html#templates-identifier-construction)
 section of the manual, backticks is the way to go.
    
    
    template dsl(typ: static[string]; code: untyped) =
      `typ Algo`()
      ...
    
    dsl "fast":
      # Call `fastAlgo`()
    
    
    Run

But the name of the procedure is interpreted as `"fast"Algo` that does not 
exist. Is it possible to remove the string quotes?

**4\. Why all this?**

I have multiple bindings to different algorithms and I want to let the user 
select the one to use. When none is selected, there is a default one. The best 
I was able to write is below that does not compile when `typ` is not specified.
    
    
    proc fastAlgo() =
      echo "fastAlgo"
    
    proc quickAlgo() =
      echo "quickAlgo"
    
    template dsl(typ: untyped; code: untyped) =
      `typ Algo`()
      code
    
    dsl fast:
      # Call `fastAlgo`()
      echo "running"
    
    dsl: <<=== type mismatch: got <void> but expected one of: template dsl(typ: 
untyped; code: untyped) first type mismatch at position: 2 missing parameter: 
code
      # Call `quickAlgo`()
      echo "running too"
    
    
    Run

How would you do this? 

Reply via email to