1

I am trying to understand how multiple inheritance in Solidity works with this snippet of code. where contract C inherits from A,B with each having function foo.

pragma solidity ^0.6.0;

contract A {
    function foo() public pure returns (uint8){
        return 1;
    }
}



contract B {
    function foo() public pure returns (uint8){
        return 2;
    }
}

contract C is A,B {

}

I got the following error when I tried to compile

contract C is A,B {
^ (Relevant source part starts here and spans across multiple lines).
browser/multiple_inheritance.sol:4:5: Definition in "A":
function foo() public pure returns (uint8){
^ (Relevant source part starts here and spans across multiple lines).
browser/multiple_inheritance.sol:12:5: Definition in "B":
function foo() public pure returns (uint8){
^ (Relevant source part starts here and spans across multiple lines).

Does it mean in Solidity 0.6 name collision for multiple inheritance is not resolvable.

0
2

This is also known as The Diamond Inheritance Problem.

In Java it is not permitted.

In C++ it can be resolved as follows:

class Base {...}

class A : public virtual Base {...}

class B : public virtual Base {...}

class C : public A, public B {...}

In Solidity it is currently (0.6.x) not supported.

See the official documentation for more details about this issue.

4
  • thank you, but the documentation suggests that it should be possible with the order from right to left, which I understand that contract C would take function foo from contract B. I don't understand how this is a diamond problem either, since there is no other contract above A and B. – Anh Dũng Lê Apr 28 '20 at 9:37
  • @AnhDũngLê: I can't see how the order would make any difference in your example, as the two base contracts (A and B) are literally the same. – goodvibration Apr 28 '20 at 9:39
  • @AnhDũngLê: And you are correct in stating that this does not illustrate the Diamond Problem precisely, but the consequences are the same. You could easily add a base contract and have both A and B inheriting from it. It wouldn't make any difference whether or not you use that base contract, because there is currently no solution for this problem in Solidity, so it's the same results with and without a base contract. In order to solve the problem in C++, that base contract IS required. – goodvibration Apr 28 '20 at 9:41
  • I mean in Python if I write class C(A,B) then the function will be searched in the order C, A, B (i.e. left to right), so when the document says the order in Solidity is from right to left I would assume `contract C is A,B' means the order of searching for function would be C, B, A. – Anh Dũng Lê Apr 28 '20 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.