Rust has a very handy dbg! macro: https://doc.rust-lang.org/std/macro.dbg.html
You right it around an expression, and it prints the text of the expression, the value, and the file and line:
let b = dbg!(a * 2) + 1;
// ^-- prints: [src/main.rs:2] a * 2 = 4
The best part is that it returns the original expression, so you can use it drop-in and remove it when no longer needed.
Is there something like that in nim?
There is not an equivalent that I know of in the stdlib, there is sugar.dump but it does not return, but here I quickly whipped up a copy:
import std/strutils
import std/sugar
template dbg*(expr: untyped): untyped =
block:
const
instInfo = instantiationInfo()
exprStr = astToStr(expr)
let val = expr
echo instInfo.fileName, "[$#:$#] " % [$instInfo.line, $instInfo.column], exprStr, " = ", val
val
var a = dbg 10 + 20 * 30
import std/macros
macro dbg(expr: untyped): untyped =
let
exprStr = expr.toStrLit
lineinfoStr = expr.lineinfo.newStrLitNode
quote do:
block:
let x = `expr`
echo "[", `lineinfoStr`, "] ", `exprStr`, " = ", x
x
let a = 2
echo dbg(a * 2) + 1
Output:
[/home/jail/prog.nim(14, 11)] a * 2 = 4
5
I don't think so, though it should be very easy to replicate:
import macros
macro dbg(val: untyped): untyped =
let
lineInfo = lineInfo(val)
exprStr = repr(val)
quote do:
let val = `val`
echo `lineInfo`, ": ", `exprStr`, " = ", val # or repr(val) depending on what you want
val
let
a = 42
b = dbg(a * 2) + 1