Merge pull request #7647 from ethereum/virtual-5424

Implement virtual keyword
This commit is contained in:
chriseth
2019-11-19 13:21:27 +01:00
committed by GitHub
91 changed files with 461 additions and 303 deletions
+4
View File
@@ -60,6 +60,8 @@ This section highlights changes that affect syntax and semantics.
which send value will revert. If you only implement the receive and not the fallback function, calling a non-existing function on your contract in error is not possible anymore. Unless you are following an upgrade or proxy
pattern, you should not need to implement the fallback function.
* Functions can now only be overridden when they are either marked with the ``virtual`` keyword or defined in an interface. When overriding a function or modifier, the new keyword ``override`` must be used. When overriding a function or modifier defined in multiple parallel bases, all bases must be listed in parentheses after the keyword like so: ``override(Base1, Base2)``.
How to update your code
=======================
@@ -81,6 +83,8 @@ This section gives detailed instructions on how to update prior code for every b
* Choose unique identifiers for variable declarations in inline assembly that do not conflict with declartions outside the inline assembly block.
* Add ``virtual`` to every non-interface function you intend to override. For single inheritance, add ``override`` to every overriding function. For multiple inheritance, add ``override(A, B, ..)``, where you list all contracts that define the overridden function in the brackets. When multiple bases define the same function, the inheriting contract must override all conflicting functions.
New Features
============
+1 -1
View File
@@ -24,7 +24,7 @@ all defined functions. The usage of an abstract contract as a base class is show
pragma solidity >=0.4.0 <0.7.0;
abstract contract Feline {
function utterance() public returns (bytes32);
function utterance() public virtual returns (bytes32);
}
contract Cat is Feline {
+11 -11
View File
@@ -42,7 +42,7 @@ Details are given in the following example.
// internal functions and state variables. These cannot be
// accessed externally via `this`, though.
contract Mortal is Owned {
function kill() public {
function kill() virtual public {
if (msg.sender == owner) selfdestruct(owner);
}
}
@@ -77,7 +77,7 @@ Details are given in the following example.
// types of output parameters, that causes an error.
// Both local and message-based function calls take these overrides
// into account.
function kill() public override {
function kill() public virtual override {
if (msg.sender == owner) {
Config config = Config(0xD5f9D8D94886E70b06E474c3fB14Fd43E2f23970);
NameReg(config.lookup(1)).unregister();
@@ -115,17 +115,17 @@ seen in the following example::
}
contract mortal is owned {
function kill() public {
function kill() public virtual {
if (msg.sender == owner) selfdestruct(owner);
}
}
contract Base1 is mortal {
function kill() public override { /* do cleanup 1 */ mortal.kill(); }
function kill() public virtual override { /* do cleanup 1 */ mortal.kill(); }
}
contract Base2 is mortal {
function kill() public override { /* do cleanup 2 */ mortal.kill(); }
function kill() public virtual override { /* do cleanup 2 */ mortal.kill(); }
}
contract Final is Base1, Base2 {
@@ -144,18 +144,18 @@ explicitly in the final override, but this function will bypass
}
contract mortal is owned {
function kill() public {
function kill() virtual public {
if (msg.sender == owner) selfdestruct(owner);
}
}
contract Base1 is mortal {
function kill() public override { /* do cleanup 1 */ super.kill(); }
function kill() public virtual override { /* do cleanup 1 */ super.kill(); }
}
contract Base2 is mortal {
function kill() public override { /* do cleanup 2 */ super.kill(); }
function kill() public virtual override { /* do cleanup 2 */ super.kill(); }
}
contract Final is Base1, Base2 {
@@ -190,7 +190,7 @@ function header as shown in this example:
contract Base
{
function foo() public {}
function foo() virtual public {}
}
contract Middle is Base {}
@@ -212,12 +212,12 @@ bases, it has to explicitly override it:
contract Base1
{
function foo() public {}
function foo() virtual public {}
}
contract Base2
{
function foo() public {}
function foo() virtual public {}
}
contract Inherited is Base1, Base2
+2 -2
View File
@@ -92,8 +92,8 @@ Yes::
pragma solidity >=0.4.0 <0.7.0;
abstract contract A {
function spam() public pure;
function ham() public pure;
function spam() public virtual pure;
function ham() public virtual pure;
}