Skip to content

Instantly share code, notes, and snippets.

@Bejofo
Last active March 18, 2023 04:07
Show Gist options
  • Save Bejofo/2cd33379b9b55f661dba47b5554d98e2 to your computer and use it in GitHub Desktop.
Save Bejofo/2cd33379b9b55f661dba47b5554d98e2 to your computer and use it in GitHub Desktop.
Argument unpacking marco for Nim
import macros
import std/strformat
from std/strutils import join
macro apply(n: typed, paramsToApply:seq|tuple): untyped =
let formal_params = getImpl(n.symbol)[3]
var variable_count:int = 0
for node in formal_params:
if node.kind == nnkIdentDefs:
for n in node:
if n.kind == nnkIdent:
variable_count+=1
var smt:seq[string] = @[]
for i in 1..variable_count:
smt.add fmt"{paramsToApply.symbol}[{i-1}]"
result = parseStmt(&"{n.symbol}({smt.join(\",\")})")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment