[R] Problem with parser and if/else
Ray Brownrigg
ray at mcs.vuw.ac.nz
Fri Nov 14 20:15:53 CET 2003
Prof Brian Ripley <ripley at stats.ox.ac.uk>
> On Fri, 14 Nov 2003, Paul Lemmens wrote:
>
> > I'm trying to grasp this: if you're saying (or are you saying) that the
> > only way to have if() know that an else will be present is to put it on the
> > same line as the closing curly brace of the if() (compound) statement. But
> > if I look at some code from, e.g., aov and lm, I see plenty violations of
> > that rule.
>
> The actual rule is given in my reference (the one that Ben Bolker did not
> bother to look up) earlier in this thread.
>
> You need to ensure that the code is syntactically incomplete when `else'
> is encountered. That will always be true inside a braced expression such
> as the bodies of the functions you quote. But at top-level, you do need
> to write
>
> if(condition) something else something_else
>
> or
>
> if(condition) {
> } else {
> }
>
> since
>
> if(condition) {
> }
> else {
> }
>
> fails the test.
>
BUT, just to make it even clearer, what does succeed interactively is
(what is effectively the same as a function body):
{
if(condition) {
"TRUE"
}
else {
"FALSE"
}
}
because the parser has to find the final closing brace before the
syntactic item is complete.
Ray Brownrigg
More information about the R-help
mailing list