[Rd] small syntax suggestion

Davor Cubranic cubranic at stat.ubc.ca
Tue Aug 24 16:42:11 CEST 2010


On August 23, 2010 01:27:24 pm Philippe Grosjean wrote:
> They have to write such a code like this:
> 
> if (x < -3) do_something
> 
> That way, there is no ambiguity. Don't you think it's important to
> write clear code, including by using spaces where it makes it easier
> to read,... and less ambiguous, as you just realize?

I fully agree, and I'm sure nobody here would dispute your advice. But 
we all sometimes make typos, and the point here is that the grammar's 
ambiguity makes for hard-to-find bugs.

So, if I may focus us back on the OP's suggestion: "that R simply warn 
about an ambiguity" in 'if' statement's comparison involving '<-[0-9]'. 
It doesn't seem like an unreasonable suggestion. For comparison, GCC 
will do the same thing with C code when the '-Wparentheses' switch if 
assignment is used in a context where a truth value is expected. (E.g., 
'if (x = 3)'.)

It's been a very long time since I looked at Yacc and Lex source, but it 
looks like function 'xxif' in gram.y is the earliest place where we have 
a whole IF statement. AFAICT, this is evaluated in 'do_if' function of 
eval.c. There, the condition is evaluated via 'asLogicalNoNA'. Could 
'do_if' instead use a function similar to 'asLogicalNoNA' but which 
issues a warning if the condition is an assignment?

Davor



More information about the R-devel mailing list