argufy icon indicating copy to clipboard operation
argufy copied to clipboard

Syntax for specifying a custom error message

Open jimhester opened this issue 10 years ago • 6 comments

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

jimhester avatar Aug 19 '15 12:08 jimhester

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.

gaborcsardi avatar Aug 19 '15 13:08 gaborcsardi

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.

gaborcsardi avatar Feb 02 '16 15:02 gaborcsardi

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 avatar Feb 02 '16 18:02 jimhester

@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.

gaborcsardi avatar Feb 02 '16 21:02 gaborcsardi

It seems that it is not possible, unfortunately. https://cran.r-project.org/doc/manuals/r-devel/R-exts.html#User_002ddefined-macros

gaborcsardi avatar Feb 02 '16 23:02 gaborcsardi

Skipping this for now, will come back to it later!

gaborcsardi avatar Feb 21 '16 23:02 gaborcsardi