9

Hello to everyone. I am looking at multiple inheritance in Solidity. I came across this example within their documentation, but it is not understandable enought for me.

Can someone explane this sentence in more detail:

The reason for this is that C requests X to override A (by specifying A, X in this order), but A itself requests to override X, which is a contradiction that cannot be resolved.

My question is why C requests X to override A, i.e. what this order of inheritance means exactly?

    pragma solidity ^0.4.0;

     contract X {}
     contract A is X {}
     contract C is A, X {}
0
7

After research I found:

With multiple inheritance, there is an issue caused by the Diamond Problem. Solidity solves this problem like Python.

Here are two links describing this problem in detail: https://docs.python.org/2/tutorial/classes.html#instance-objects https://www.python.org/download/releases/2.3/mro/

So, the order of inheritance is important in order to avoid the Diamond problem. Order should be: from classes on the top level of inheritance to the classes on lower levels

1
2

Best explained in the solidity docs on Multiple Inheritance and Linearization:

You have to list the direct base contracts in the order from “most base-like” to “most derived”. Note that this order is the reverse of the one used in Python.

Another simplifying way to explain this is that when a function is called that is defined multiple times in different contracts, the given bases are searched from right to left (left to right in Python) in a depth-first manner, stopping at the first match. If a base contract has already been searched, it is skipped.

0

In multi-inheritance, the order of base contracts is very important. When a contract inherits from multiple contracts, there will be only one contract created. Solidity copies code from all inherited contracts into inheriting contract. If inheritance leads to the name conflicts of modifier or function in one contract, an error will occur. By the same token, if there are name conflicts between event and modifier, or between event and function in contract, an error will be triggered.

Solidity supports multiple inheritance, meaning that one contract can inherit several contracts. Multiple inheritance introduces ambiguity called Diamond Problem: if two or more base contracts define the same function, which one should be called in the child contract? Solidity deals with this ambiguity by using reverse C3 Linearization (an algorithm used primarily to obtain the order in which methods should be inherited in the presence of multiple inheritance), which sets a priority between base contracts. That way, base contracts have different priorities, so the order of inheritance matters. Neglecting inheritance order can lead to unexpected behavior. When inheriting multiple contracts, especially if they have identical functions, a developer should carefully specify inheritance in the correct order.

The rule of thumb is to inherit contracts from more/general/tomore/specific/.

A programming language supporting multiple inheritance should solve several problems. One of them is diamond inheritance problem. Solidity solution for Diamond Problem is based on Python, which uses C3_linearization mechanism to force converting base contracts to a directed acyclic graph (DAG).

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.