[Rd] switch() disallowing multiple default values Re: Bug in parseNamespaceFile or switch( , ... ) ?
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sun Nov 28 23:12:32 CET 2010
On Sun, 28 Nov 2010, Duncan Murdoch wrote:
> I've now committed changes in R-devel and R-patched to detect cases where a
> call to switch() contains multiple unnamed alternatives. The code only
> complains if the EXPR argument is a character string; unnamed alternatives
> are fine with numeric switching.
>
> Adding this check turned up 3 more typos like this in the base code besides
> the one in parseNamespaceFile. I expect it will turn up quite a few more in
> CRAN and Bioconductor packages.
>
> Please let me know right away if you've got correct code that generates the
> warnings or errors.
Most of the errors seem to be of the form (here from Brobdingnag)
trunc = callGeneric(as.complex(x)),,,
that is, empty arguments to switch.
>
> Duncan Murdoch
>
>
> In R-devel they're an error, in R-patched they'll just give a warning.
>
> On 27/11/2010 7:09 PM, Duncan Murdoch wrote:
>> On 27/11/2010 6:50 PM, Duncan Murdoch wrote:
>>> On 27/11/2010 5:58 PM, Charles C. Berry wrote:
>>>>
>>>> parseNamespaceFile() doesn't seem to detect misspelled directives.
>>>> Looking
>>>> at its code I see
>>>>
>>>> switch(as.character(e[[1L]]),
>>>>
>>>> <lots of args omitted here>,
>>>>
>>>> stop(gettextf("unknown namespace directive: %s",
>>>> deparse(e)), call. = FALSE, domain = NA))
>>>>
>>>> but this doesn't seem to function as I expect, viz. to stop with an error
>>>> if I type a wrong directive.
>>>
>>> You're right, there was a typo in parseNamespaceFile. (The typo was in
>>> this line:
>>>
>>> "=", "<-" = {
>>>
>>> This should have been
>>>
>>> "=" =, "<-" = {
>>>
>>> Without the extra = sign, the "=" was taken as the default value of the
>>> switch, and the stop() was never reached.
>>>
>>> Conceivably switch() should complain if it is called with more than one
>>> default.
>>
>> I suspect when I fix this it's going to flush out some typos in packages
>> on CRAN...
>>
>> Duncan Murdoch
>>
>>
>>> Duncan Murdoch
>>>
>>>>
>>>> Details:
>>>>> # create dummy NAMESPACE file with two bad / one good directives
>>>>> cat("blah( nada )\nblee( nil )\nexport( outDS )\n",file="NAMESPACE")
>>>>> readLines("NAMESPACE")
>>>> [1] "blah( nada )" "blee( nil )" "export( outDS )"
>>>>> parseNamespaceFile("",".") # now parse it
>>>> $imports
>>>> list()
>>>>
>>>> $exports
>>>> [1] "outDS"
>>>>
>>>> $exportPatterns
>>>> character(0)
>>>>
>>>> $importClasses
>>>> list()
>>>>
>>>> $importMethods
>>>> list()
>>>>
>>>> $exportClasses
>>>> character(0)
>>>>
>>>> $exportMethods
>>>> character(0)
>>>>
>>>> $exportClassPatterns
>>>> character(0)
>>>>
>>>> $dynlibs
>>>> character(0)
>>>>
>>>> $nativeRoutines
>>>> list()
>>>>
>>>> $S3methods
>>>> [,1] [,2] [,3]
>>>>
>>>>>
>>>>
>>>> So, it picked up 'export' and ignored the other two lines.
>>>>
>>>>
>>>> Chuck
>>>>
>>>> p.s.
>>>>
>>>>> sessionInfo()
>>>> R version 2.12.0 (2010-10-15)
>>>> Platform: i386-apple-darwin9.8.0/i386 (32-bit)
>>>>
>>>> locale:
>>>> [1] C
>>>>
>>>> attached base packages:
>>>> [1] stats graphics grDevices utils datasets methods base
>>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> Charles C. Berry Dept of Family/Preventive
>>>> Medicine
>>>> cberry at tajo.ucsd.edu UC San Diego
>>>> http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego
>>>> 92093-0901
>>>>
>>>> ______________________________________________
>>>> R-devel at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-devel
mailing list