Merge pull request #12901 from aathan/clarify-base-contract-constructors

Clarify base contract constructors (re request of PR #12872)
This commit is contained in:
Kamil Śliwak 2022-04-07 13:33:05 +02:00 committed by GitHub
commit 0dd7f9c871
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -443,7 +443,7 @@ cannot be assigned valid values from outside but only through the constructors o
``internal`` or ``public``.
.. index:: ! base;constructor
.. index:: ! base;constructor, inheritance list, contract;abstract, abstract contract
Arguments for Base Constructors
===============================
@ -467,11 +467,20 @@ derived contracts need to specify all of them. This can be done in two ways:
constructor() {}
}
// or through a "modifier" of the derived constructor.
// or through a "modifier" of the derived constructor...
contract Derived2 is Base {
constructor(uint y) Base(y * y) {}
}
// or declare abstract...
abstract contract Derived3 is Base {
}
// and have the next concrete derived contract initialize it.
contract DerivedFromDerived is Derived3 {
constructor() Base(10 + 10) {}
}
One way is directly in the inheritance list (``is Base(7)``). The other is in
the way a modifier is invoked as part of
the derived constructor (``Base(y * y)``). The first way to
@ -484,7 +493,12 @@ inheritance list or in modifier-style in the derived constructor.
Specifying arguments in both places is an error.
If a derived contract does not specify the arguments to all of its base
contracts' constructors, it will be abstract.
contracts' constructors, it must be declared abstract. In that case, when
another contract derives from it, that other contract's inheritance list
or constructor must provide the necessary parameters
for all base classes that haven't had their parameters specified (otherwise,
that other contract must be declared abstract as well). For example, in the above
code snippet, see ``Derived3`` and ``DerivedFromDerived``.
.. index:: ! inheritance;multiple, ! linearization, ! C3 linearization