Selection of method in callNextMethod(); multiple inheritance
1
0
Entering edit mode
@stolarczykmichal93-17545
Last seen 5.6 years ago

Hi,

I am implementing a class that inherits from two other classes (see the example below). And just wanted to ask whether it is possible to specify which show() method will be used (A or B) when calling the new show() method associated with class C.

I am aware that the call to callNextMethod() results in the call to the first inherited method.

setClass(
  "A",slots = c(slot1 = "character")
)

setMethod(
  f = "show",
  signature = "A",
  definition = function(.Object) {
    print("Show class A")
  }
)

setClass(
  "B",slots = c(slot1 = "character")
)

setMethod(
  f = "show",
  signature = "B",
  definition = function(.Object) {
    print("Show class B")
  }
)

setClass(
  "C",slots = c(slot1 = "character"),contains = c("A","B")
)

setMethod(
  f = "show",
  signature = "C",
  definition = function(.Object) {
    callNextMethod()
  }
)

new("C")
class multiple inheritance callnextmethod • 755 views
ADD COMMENT
2
Entering edit mode
@martin-morgan-1513
Last seen 2 days ago
United States

Questions about package development should be on the bioc-devel mailing list.

I'd say no, there is no way to influence the way method dispatch works. There are a number of hacks that one could imagine, e.g., using selectMethod() to choose your desired method, and calling that instead of callNextMethod(), but probably the answer is somewhere else, either in class redesign or code refactoring (e.g., creating a helper function that does that you would like to share between methods).

ADD COMMENT
0
Entering edit mode

selectMethod() will do the job, thanks!

Michal

ADD REPLY

Login before adding your answer.

Traffic: 531 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6