Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top