From wdunlap at tibco.com  Tue Aug 20 23:00:49 2013
From: wdunlap at tibco.com (William Dunlap)
Date: Tue, 20 Aug 2013 21:00:49 +0000
Subject: [Rd] Extending suggestion for stopifnot
In-Reply-To: <CAPr7RtX3b5XBzgn1XT3nPcvX71G=W1CpK=tK1T3zz4zWW=pQNQ@mail.gmail.com>
References: <CAPr7RtX3b5XBzgn1XT3nPcvX71G=W1CpK=tK1T3zz4zWW=pQNQ@mail.gmail.com>
Message-ID: <E66794E69CFDE04D9A70842786030B931C3303EB@PA-MBX01.na.tibco.com>

> my own programming variant looks even nicer,
>    is.matrix(m) %or% "m is not a matrix but a {{class(m)}}"

But it does not act nicely.  All %anything% constructs have the
same precedence, that of %*%.   Hence
   x==3 %or% "x is not three"
is parsed as
  x == ( 3 %or% "x is not three" )
which is not what I think you want.

Use standard functional notation, e.g., ensureThat(condition, message), and
you don't get into parsing trouble.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf
> Of ivo welch
> Sent: Tuesday, August 20, 2013 11:42 AM
> To: r-devel at r-project.org List
> Subject: [Rd] Extending suggestion for stopifnot
> 
> I am using a variant of stopifnot a lot.  can I suggest that base R
> extends its functionality?  I know how to do this for myself.  this is
> a suggestion for beginners and students.  I don't think it would break
> anything.
> 
> first, I think it would be more useful if it had an optional character
> string, so users could write
> 
>   stopifnot( is.matrix(m), "m is not a matrix" )
> 
> this would mean that stopifnot would have to detect whether the last
> argument is a string.  (I think stopifnot should have had only one
> condition, and one should have used all() to test multiple conditions,
> but this is a bridge that was already crossed.)  upon failure,
> stopifnot should print the character string.  that's it.
> 
> 
> A second enhancement would be a "smart string", which knows that
> everything inside {{...}} should be evaluated.
> 
>   stopifnot( is.matrix(m), "m is not a matrix, but a {{class(m)}}" )
> 
> 
> my own programming variant looks even nicer,
> 
>    is.matrix(m) %or% "m is not a matrix but a {{class(m)}}"
> 
> but requesting base R to add the %and% and %or% (or, better yet, 'and'
> and 'or') operators by default would be pushing my luck.
> 
> /iaw
> 
> 
> ----
> Ivo Welch (ivo.welch at gmail.com)
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Extending suggestion for stopifnot
William Dunlap