Last active
March 18, 2023 04:07
-
-
Save Bejofo/2cd33379b9b55f661dba47b5554d98e2 to your computer and use it in GitHub Desktop.
Argument unpacking marco for Nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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