Skip to content

Commit 732f027

Browse files
author
maechler
committed
show( selectMethod(..) ) now prints group generic if it differs
git-svn-id: https://svn.r-project.org/R/trunk@88033 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent d20a5d3 commit 732f027

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@
253253
\item \code{model.frame()} produces more informative error messages
254254
in some cases when variables in the formula are not found, thanks to
255255
\I{Ben Bolker}'s \PR{18860}.
256+
257+
\item \code{selectMethod(f, ..)} now keeps the function name if the
258+
function belongs to of a group generic and the method is for the
259+
generic.
256260
}
257261
}
258262

src/library/methods/R/Methods.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/methods/R/Methods.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2024 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -774,14 +774,20 @@ selectMethod <-
774774
cat("* mlist environment with", length(mlist),"potential methods\n")
775775
if(length(signature) < nsig)
776776
signature[(length(signature)+1):nsig] <- "ANY"
777+
inGroups <- length(fdef@group) > 0L # " I belong to a group "
778+
maybeGrp <- function(meth) { # meth: a function, possibly primitive, or method
779+
if(inGroups && isS4(meth) && meth@generic != fdef@generic)
780+
attr(meth@generic, "orig") <- fdef@generic
781+
meth
782+
}
777783
if(identical(fdef@signature, "...")) {
778784
method <- .selectDotsMethod(signature, mlist,
779785
if(useInherited) getMethodsForDispatch(fdef, inherited = TRUE))
780786
if(is.null(method) && !optional)
781787
stop(gettextf("no method for %s matches class %s",
782788
sQuote("..."), dQuote(signature)),
783789
domain = NA)
784-
return(method)
790+
return(maybeGrp(method))
785791
}
786792
method <- .findMethodInTable(signature, mlist, fdef)
787793
if(is.null(method)) {
@@ -805,7 +811,7 @@ selectMethod <-
805811
## else list() : just look in the direct table
806812

807813
if(length(methods))
808-
return(methods[[1L]])
814+
return(maybeGrp(methods[[1L]]))
809815
else if(optional)
810816
return(NULL)
811817
else stop(gettextf("no method found for signature %s",

src/library/methods/R/show.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/methods/R/show.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2024 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -80,8 +80,13 @@ show <- function(object) showDefault(object)
8080
"" else paste0(" (Class ", classLabel(cl),")")
8181
cat("Method Definition",nonStandard,":\n\n", sep = "")
8282
show(object@.Data)
83+
cat("\n") # in both cases
84+
if(isGroup(g <- object@generic) &&
85+
!is.null(og <- c(attr(g,"orig"))) && (g <- c(g)) != og)
86+
cat(sprintf("Generic: target: \"%s\", defined: \"%s\"\n", og, g))
87+
## e.g., Generic: target: "-", defined: "Arith"
8388
mm <- methodSignatureMatrix(object)
84-
cat("\nSignatures:\n")
89+
cat("Signatures:\n")
8590
print(mm)
8691
},
8792
where = envir)

src/library/methods/man/S4groupGeneric.Rd

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/methods/man/S4groupGeneric.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2016 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{S4groupGeneric}
@@ -123,14 +123,11 @@ There are also
123123
\references{
124124
Chambers, John M. (2016)
125125
\emph{Extending R},
126-
Chapman & Hall.
127-
(Chapters 9 and 10.)
128-
126+
Chapman & Hall. (Chapters 9 and 10.)
129127

130128
Chambers, John M. (2008)
131129
\emph{Software for Data Analysis: Programming with R}
132130
Springer. (Section 10.5)
133-
134131
}
135132
\seealso{ The function \code{\link{callGeneric}} is nearly always
136133
relevant when writing a method for a group generic. See the
@@ -141,6 +138,7 @@ There are also
141138
\examples{
142139
setClass("testComplex", slots = c(zz = "complex"))
143140
## method for whole group "Complex"
141+
getGroupMembers("Complex") # "Arg" "Conj" "Im" "Mod" "Re"
144142
setMethod("Complex", "testComplex",
145143
function(z) c("groupMethod", callGeneric(z@zz)))
146144
## exception for Arg() :
@@ -150,6 +148,7 @@ z1 <- 1+2i
150148
z2 <- new("testComplex", zz = z1)
151149
stopifnot(identical(Mod(z2), c("groupMethod", Mod(z1))))
152150
stopifnot(identical(Arg(z2), c("ArgMethod", Arg(z1))))
151+
selectMethod("Re", signature = "testComplex") # shows Generic: .. "Re" & .."Complex"
153152
\dontshow{
154153
removeMethods("Complex")
155154
removeMethods("Arg")

src/library/methods/tests/testGroupGeneric.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ a <- new("A")
66
setMethod("Logic", c("A", "A"), function(e1, e2) FALSE)
77
res0 <- a & a # inherit &,A,A-method
88
setMethod("Logic", c("A", "A"), function(e1, e2) TRUE)
9-
stopifnot(a & a)
9+
stopifnot(!res0, a & a)
10+
## feature in R >= 4.5.0: these get same method, but slightly differing result:
11+
(sa <- selectMethod("&", c("A", "A"))) # printing "Generic: ..."
12+
(sL <- selectMethod("Logic", c("A", "A"))) #
13+
stopifnot(identical(sa@generic,
14+
structure("Logic", package = "base",
15+
orig = structure("&", package = "base"))))
1016

1117
removeMethod("Logic", c("A", "A"))
1218
stopifnot(logical() == a & a)

0 commit comments

Comments
 (0)