Skip to content

Instantly share code, notes, and snippets.

View geekrelief's full-sized avatar
🤖

Don-Duong Quach geekrelief

🤖
View GitHub Profile
@geekrelief
geekrelief / macro_nilable.nim
Created January 22, 2022 19:34
Nim: Check if a type is nilable in a macro.
import std/[ macros, strformat ]
proc parseParts(x: NimNode): seq[NimNode]
proc parseDotExpr(x: NimNode): seq[NimNode] =
result.add parseParts(x[0])
result.add x
proc parseBracketExpr(x: NimNode): seq[NimNode] =
result.add parseParts(x[0])
@geekrelief
geekrelief / here.nim
Last active June 28, 2024 18:22
A Nim debugEcho with file, line number, and callsite.
import std/macros
# calls debugEcho with the lineInfo
macro here*(x: varargs[typed, `$`]):untyped {.noSideEffect.} =
{.cast(noSideEffect), warning[Deprecated]:off.}: # callsite is deprecated, there's no alternative: https://github.com/nim-lang/RFCs/issues/387
result = newTree(nnkCommand, ident "debugEcho")
result.add newStrLitNode("--- " & x[0].lineInfo & " ---\n")
result.add newStrLitNode(" " & callsite().toStrLit().strVal & " -> \n")
for c in x:
result.add c