Skip to content
Prev 53484 / 63424 Next

stopifnot() does not stop at first non-TRUE argument

Hi,
On 05/15/2017 10:41 AM, luke-tierney at uiowa.edu wrote:
My main use case for using stopifnot() is argument checking. In that
context, I like the conciseness of

   stopifnot(
     A,
     B,
     ...
   )

I think it's a common use case (and a pretty natural thing to do) to
order/organize the expressions in a way such that it only makes sense
to continue evaluating if all was OK so far e.g.

   stopifnot(
     is.numeric(x),
     length(x) == 1,
     is.na(x)
   )

At least that's how things are organized in the stopifnot() calls that
accumulated in my code over the years. That's because I was convinced
that evaluation would stop at the first non-true expression (as
suggested by the man page). Until recently when I got a warning issued
by an expression located *after* the first non-true expression. This
was pretty unexpected/confusing!

If I can't rely on this "sequencing" feature, I guess I can always
do

   stopifnot(A)
   stopifnot(B)
   ...

but I loose the conciseness of calling stopifnot() only once.
I could also use

   stopifnot(A && B && ...)

but then I loose the conciseness of the error message i.e. it's going
to be something like

   Error: A && B && ... is not TRUE

which can be pretty long/noisy compared to the message that reports
only the 1st error.

Conciseness/readability of the single call to stopifnot() and
conciseness of the error message are the features that made me
adopt stopifnot() in the 1st place. If stopifnot() cannot be revisited
to do "sequencing" then that means I will need to revisit all my calls
to stopifnot().
Why not. Still better than the current situation. But only if that
semantic seems more useful to people. Would be sad if usefulness
of one semantic or the other was decided based on trickiness of
implementation.

Thanks,
H.

  
    

Thread (22 messages)

Hervé Pagès stopifnot() does not stop at first non-TRUE argument May 2 Peter Dalgaard stopifnot() does not stop at first non-TRUE argument May 3 Hervé Pagès stopifnot() does not stop at first non-TRUE argument May 3 Hervé Pagès stopifnot() does not stop at first non-TRUE argument May 3 Martin Maechler stopifnot() does not stop at first non-TRUE argument May 15 Serguei Sokol stopifnot() does not stop at first non-TRUE argument May 15 Serguei Sokol stopifnot() does not stop at first non-TRUE argument May 15 Martin Maechler stopifnot() does not stop at first non-TRUE argument May 15 Peter Dalgaard stopifnot() does not stop at first non-TRUE argument May 15 Serguei Sokol stopifnot() does not stop at first non-TRUE argument May 15 Martin Maechler stopifnot() does not stop at first non-TRUE argument May 15 Peter Dalgaard stopifnot() does not stop at first non-TRUE argument May 15 Martin Maechler stopifnot() does not stop at first non-TRUE argument May 15 Serguei Sokol stopifnot() does not stop at first non-TRUE argument May 15 Luke Tierney stopifnot() does not stop at first non-TRUE argument May 15 Hervé Pagès stopifnot() does not stop at first non-TRUE argument May 15 Hervé Pagès stopifnot() does not stop at first non-TRUE argument May 15 Martin Maechler stopifnot() does not stop at first non-TRUE argument May 16 Serguei Sokol stopifnot() does not stop at first non-TRUE argument May 16 Luke Tierney stopifnot() does not stop at first non-TRUE argument May 16 Luke Tierney stopifnot() does not stop at first non-TRUE argument May 16 Martin Maechler stopifnot() does not stop at first non-TRUE argument May 16