jmc@R-project.org
2004-Oct-29 22:25 UTC
[Rd] (PR#7320) Internal function isUME() in findGeneric() is
Jeffrey J. Hallman wrote:>OK, I looked at the documentation and you're right about that. However, >I'm curious about why the first argument to UseMethod is ever necessary. >Is there ever a good reason for it to be something other than the name >of the calling function? (Wouldn't that lead to confusing code?) If >not, why bother with it at all? > >I've become spoiled by programming a lot recently in Smalltalk, which is >the cleanest language I've used yet. > >It's not simply a matter of cleanliness. Different languages have different computing models, which allow different programming techniques. The model underlying R uses ideas from functional programming, and in particular the idea that the meaning of a function should be as clear as possible from the function itself. UseMethod() clearly tells you less than UseMethod("coef"). More practically, it's not always clear what "the name of the calling function" is, or that such a name has anything to do with the computation. It's the function _as an object_ that should determine what happens. There's nothing in the language that says you shouldn't make a vector of function objects, (f[1] <- coef; f[2] <- resid, etc. ) and then call (f[i])(myModel). In S3 methods, the generic function is just a plain "function" object, so any convention for hiding "the name" in the object must be somewhat of a hack. In S4, the generic function class could (and does) have a slot in the object to identify the generic, so the issue is less obvious. But one could still argue for using an explicit name when dispatching, on the grounds of clarity.>Jeff > > >Prof Brian Ripley <ripley@stats.ox.ac.uk> wrote: > > br> The issue is the use of UseMethod(), which you claim to be valid. As it > br> is inconsistent with the prime documentation, the help page, I would > br> question if it is `perfectly legal'. I expect the author of isUME > br> implemented the behaviour described on the help page. > > br> It is documented in S-PLUS as *strongly discouraged*, and although it is > br> documented in R-lang, I believe the right thing to do is to make the code > br> agree with the prime documentation. > > br> I see no uses of UseMethod() in the R sources, and on CRAN only > br> one in a comment (and none in BioC). > > br> On Thu, 28 Oct 2004 jhallman@frb.gov wrote: > > >> Full_Name: Jeff Hallman > >> Version: 2.0 > >> OS: Linux > >> Submission from: (NULL) (132.200.32.34) > >> > >> > >> The function findGeneric() in the utils namespace contains this internal > >> function: > >> > >> isUME <- function(e) { > >> if (is.call(e) && (is.name(e[[1]]) || is.character(e[[1]]))) { > >> switch(as.character(e[[1]]), UseMethod = as.character(e[[2]]), > >> "{" = isUMEbrace(e), "if" = isUMEif(e), "") > >> } > >> else "" > >> } > >> > >> This fails if the generic called UseMethod() without an argument, which is > >> perfectly legal. Changing it to this seems to work: > >> > >> isUME <- function(e) { > >> if(is.call(e) &&(is.name(e[[1]]) || is.character(e[[1]]))) { > >> switch(as.character(e[[1]]), > >> UseMethod = ifelse(length(e) == 1, fname, as.character(e[[2]])), > >> "{" = isUMEbrace(e), > >> "if" = isUMEif(e), > >> "") > >> } > >> else "" > >> } > >> > >> > >> Jeff > >> > >> ______________________________________________ > >> R-devel@stat.math.ethz.ch mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-devel > >> > >> > > br> -- > br> Brian D. Ripley, ripley@stats.ox.ac.uk > br> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > br> University of Oxford, Tel: +44 1865 272861 (self) > br> 1 South Parks Road, +44 1865 272866 (PA) > br> Oxford OX1 3TG, UK Fax: +44 1865 272595 > >______________________________________________ >R-devel@stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-devel > >
Reasonably Related Threads
- Internal function isUME() in findGeneric() is wrong (PR#7320)
- format.POSIXlt drops characters following percent sign (PR#8976)
- format.POSIXlt drops characters following percent sign (PR#8975)
- foreign::read.ssd and long names (PR#9631)
- mistake in FLAC++ metadata interface?