I am embarrassed to admit that today I surrendered to Julia’s meta programming x)
What I want to do is write a macro @check a b c … which checks the varargs input in turn and prints an informative error message with both name (symbol) and value of the item that caused the trouble. So, If I want to print the variables name
as well, I guess I’ll need a macro?
macro check(x) name = string(x) :($x < 0 ? error(@sprintf("%s must be >= 0, is %.5f", $name, $x)) : true) end
works fine for one input but I simply cannot get it to work with multiple inputs. Any attempt to loop over an input tuple x…
macro checks(x...) tmp = Expr(:block) for xx in x name = string(xx) append!(tmp.args, :($xx < 0 ? error(@sprintf("%s must be >= 0, is %.5f", $name, $xx)) : true)) end return(tmp) end
fails with: no method matching length(:Expr) .