5

I am creating S4 classes in R.

I read in https://github.com/hadley/devtools/wiki/S4

Note that S4 supports multiple inheritance, but this should be used with extreme caution as it makes method lookup extremely complicated.

What is method lookup and why is it more complex with multiple inheritance?

RockScience
  • 17,932
  • 26
  • 89
  • 125
  • Just making sure you're aware of the numerous S4 resources: http://stackoverflow.com/questions/4143611/sources-on-s4-objects-methods-and-programming-in-r – Roman Luštrik Jan 12 '12 at 08:41

1 Answers1

11

When you type f(x), with x belonging to several classes (say, A, B and C), the computer has to decide which f method to call (that from class A, B, or C): this is called "method lookup".

Multiple inheritance often poses problems when the code evolves.

Imagine you have written two base classes A and B, and class C inherits from both. Everything works fine. A few months later, a developer, who uses class A, and is completely unaware of classes B and C (he does not need them), adds a new method to class A. Unbeknownst to him, there is already a method with the same name in class B. What happens to objects of class C? Will the method from A or B be used? In some languages, the code may fail, in others you can have an undefined behaviour and a very hard-to-catch bug.

Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
  • 3
    I think your explanation is a bit confused about s4 methods - they belong to functions, not classes. – hadley Jan 13 '12 at 02:36