src/littlesugar/expandFirst

Search:
Group by:

Templates

template expandFirst(macroOrTemplateCall, body: untyped): untyped

Expand macroOrTemplateName inside body before expanding other macros or templates.

macroOrTemplateCall cannot take template or macro call that takes non-compile time arguments.

Example:

import macros

macro putEcho(body: untyped): untyped =
  # This sample macro replace `e args` to `echo args`
  result = body.copyNimNode
  for i in body:
    if i.kind == nnkCommand and i.len > 0 and i[0].eqIdent("e"):
      var c = newCall bindSym"echo"
      c.add i[1..^1]
      result.add c
    else:
      result.add i

putEcho:
  e "Hello"

template someCode(): untyped =
  e "Hello"

#[
# This doesn't work as someCode template is expanded after putEcho is expanded.
putEcho:
  someCode()
]#

expandFirst(someCode()):
  putEcho:
    someCode()