Skip to content
Prev 7286 / 12125 Next

[R-pkg-devel] Workaround for code/documentation mismatch

When not preceded by an expression wrapped in do(), %while% would throw an
error "do while loop must begin with 'do'". The function %while% looks like

`%while%` <- function (expr, cond)
invisible(.Call(C_do.while, substitute(expr), substitute(cond),
parent.frame()))

and the corresponding C function looks like

SEXP do_dowhile(SEXP expr, SEXP cond, SEXP rho)
{
    /* we're looking for an expression of the form
       do ( expr )
       do ( { expr1 ; expr2 } )

       there must NOT be a tag on the second item, the expression within
'do'
       that is, we reject expressions of the form

       do (var = expr)
       do (var = { expr1 ; expr2 } ) */
    if (TYPEOF(expr) != LANGSXP || CAR(expr) != install("do"))
        error("do while loop must begin with 'do'");
    else if (xlength(expr) != 2 || !isNull(TAG(CDR(expr))))
        error("invalid 'expr'");

The 'do' part of the expression is irrelevant and later removed, it's only
required because it makes the syntax look more like other languages.
So the proper way to use %while% is
expr %while% cond
where expr is wrapped with do(), but I think it would be far more
understandable in the usage documentation to have it look like
do(expr) %while% (cond)

If it helps at all with context, I'll provide the R and C scripts I'm using.

On Tue, Aug 10, 2021 at 11:47 PM Hugh Parsonage <hugh.parsonage at gmail.com>
wrote: