I'm trying to debug a compilation problem: the generic parameter of a proc doesn't seem to match my expectation, so I'd like to write something like this:
let b: byte = 42'u8
proc test*[T](t: T): bool =
when sizeof(T) == 1:
result = true # ...
else:
{.error: "invalid parameter size: " & $sizeof(T).}
test(b)
But the compiler returns this:
Error: invalid pragma: error: "invalid parameter size: " & $ sizeof(T)
I tried searching in the Nim codebase, but found no example. So is it impossible?
find . -name "*.nim" | xargs fgrep "{.error"
You can use asserts:
let b: byte = 42'u8
let c = 42'u16
proc test*[T](t: T): bool =
assert(sizeof(T) == 1, "invalid parameter size: " & $sizeof(T))
result = true
echo test(b)
echo test(c)
Mmh there is an error proc in macros that I think does that: https://nim-lang.org/docs/macros.html#error,string,NimNode
edit: Nope, the proc only works in compile time contexts