argufy
argufy copied to clipboard
Syntax for specifying a custom error message
I think it would be useful to have an extension of the syntax to allow users to specify a custom error message. i.e.
fun <- function(x = ? is.numeric : "X must be a number!") x
Using : makes the syntax like C's ternary if which seems nice. However I am not sure how challenging parsing the : would be (disamgibuating normal uses of :). So if it proves too messy reusing the ? again would work as well and maybe gives the expressions a nice symmetry, I am fine with either method.
fun <- function(x = ? is.numeric ? "X must be a number!") x
In this case I guess the implementation would change from just wrapping the checks in stopifnot() to something like
if (!all(checks)) {
stop(message, call. = FALSE)
}
This also ties into #1
Hmmm. I think this is nice in general, but maybe we want to do sg with #1 first.
Also, often, it is just as good, or maybe even better to define you own assertion, and then the assertion can give any error message it wants. I know this does not always works well, so I think custom error messages will be ultimately useful for one-time error messages.
So, yeah, if you want to add it, it is fine with me. We might need to change the implementation somewhat if #1 and/or #6 change.
I think the ? syntax is way less ambiguous and much easier to implement. Although ?~ ... ? might be tricky.
Btw. I am still caught up with my move, and this probably won't change until September, so I'll be less responsive until then. Will try to fix the r-builder issue, though.
This would be still nice to have with the new syntax. E.g. sg like this might make sense:
#' @param x \assert{is.numeric; "x must be a number"} Blah-blah...
I am not a big fan of the semicolon, though. The syntax can be anything we like, because it is passed on as a string, and only parsed later.
We could use syntax similar to figures
\figure{logo.jpg}{Our logo}
E.g.
#' @param x \assert{is.numeric}{x must be a number} Blah-blah...
@jimhester That's a good idea, but I am not sure if it is possible to define a macro with variable number of arguments. I'll try.
It seems that it is not possible, unfortunately. https://cran.r-project.org/doc/manuals/r-devel/R-exts.html#User_002ddefined-macros
Skipping this for now, will come back to it later!