Created
August 24, 2017 10:04
-
-
Save anonymous/ef01c377c45e615fdc163dd53d328f7f to your computer and use it in GitHub Desktop.
Snippet from https://play.nim-lang.org
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 strutils, macros | |
proc printImpl*(objects: openarray[string], sep = " ", endl = "\n", | |
file=stdout, flush=false) = | |
file.write(objects.join(sep)) | |
file.write(endl) | |
if flush: | |
file.flushFile() | |
macro print*(data: varargs[untyped]): untyped = | |
var objects = newTree(nnkBracket) | |
var args = newTree(nnkArglist) | |
for arg in data: | |
if arg.kind == nnkExprEqExpr: | |
# Add keyword argument | |
args.add arg | |
else: | |
# Stringify automatically | |
objects.add newCall("$", arg) | |
objects = prefix(objects, "@") | |
result = quote do: | |
printImpl(`objects`, `args`) | |
print 5 | |
print(1, 2, 3, 4) | |
print("hello world", 1, 2, sep = "XXX", endl="\n\n") | |
print(3, endl="test") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment