mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Documentation.
This commit is contained in:
@@ -18,7 +18,7 @@ Not all types for constants and immutables are implemented at this time. The onl
|
||||
::
|
||||
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
pragma solidity >0.6.4 <0.8.0;
|
||||
pragma solidity >0.6.99 <0.8.0;
|
||||
|
||||
contract C {
|
||||
uint constant X = 32**22 + 8;
|
||||
@@ -28,7 +28,7 @@ Not all types for constants and immutables are implemented at this time. The onl
|
||||
uint immutable maxBalance;
|
||||
address immutable owner = msg.sender;
|
||||
|
||||
constructor(uint _decimals, address _reference) public {
|
||||
constructor(uint _decimals, address _reference) {
|
||||
decimals = _decimals;
|
||||
// Assignments to immutables can even access the environment.
|
||||
maxBalance = _reference.balance;
|
||||
|
||||
@@ -48,7 +48,7 @@ This means that cyclic creation dependencies are impossible.
|
||||
|
||||
// This is the constructor which registers the
|
||||
// creator and the assigned name.
|
||||
constructor(bytes32 _name) public {
|
||||
constructor(bytes32 _name) {
|
||||
// State variables are accessed via their name
|
||||
// and not via e.g. `this.owner`. Functions can
|
||||
// be accessed directly or through `this.f`,
|
||||
|
||||
@@ -18,10 +18,10 @@ if they are marked ``virtual``. For details, please see
|
||||
::
|
||||
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
pragma solidity >=0.5.0 <0.8.0;
|
||||
pragma solidity >0.6.99 <0.8.0;
|
||||
|
||||
contract owned {
|
||||
constructor() public { owner = msg.sender; }
|
||||
constructor() { owner = msg.sender; }
|
||||
address payable owner;
|
||||
|
||||
// This contract only defines a modifier but does not use
|
||||
@@ -63,7 +63,7 @@ if they are marked ``virtual``. For details, please see
|
||||
mapping (address => bool) registeredAddresses;
|
||||
uint price;
|
||||
|
||||
constructor(uint initialPrice) public { price = initialPrice; }
|
||||
constructor(uint initialPrice) { price = initialPrice; }
|
||||
|
||||
// It is important to also provide the
|
||||
// `payable` keyword here, otherwise the function will
|
||||
|
||||
@@ -39,11 +39,11 @@ Details are given in the following example.
|
||||
::
|
||||
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
pragma solidity >=0.6.0 <0.8.0;
|
||||
pragma solidity >0.6.99 <0.8.0;
|
||||
|
||||
|
||||
contract Owned {
|
||||
constructor() public { owner = msg.sender; }
|
||||
constructor() { owner = msg.sender; }
|
||||
address payable owner;
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ Details are given in the following example.
|
||||
// also a base class of `Destructible`, yet there is only a single
|
||||
// instance of `owned` (as for virtual inheritance in C++).
|
||||
contract Named is Owned, Destructible {
|
||||
constructor(bytes32 name) public {
|
||||
constructor(bytes32 name) {
|
||||
Config config = Config(0xD5f9D8D94886E70b06E474c3fB14Fd43E2f23970);
|
||||
NameReg(config.lookup(1)).register(name);
|
||||
}
|
||||
@@ -106,8 +106,8 @@ Details are given in the following example.
|
||||
|
||||
|
||||
// If a constructor takes an argument, it needs to be
|
||||
// provided in the header (or modifier-invocation-style at
|
||||
// the constructor of the derived contract (see below)).
|
||||
// provided in the header or modifier-invocation-style at
|
||||
// the constructor of the derived contract (see below).
|
||||
contract PriceFeed is Owned, Destructible, Named("GoldFeed") {
|
||||
function updateInfo(uint newInfo) public {
|
||||
if (msg.sender == owner) info = newInfo;
|
||||
@@ -127,10 +127,10 @@ destruction request. The way this is done is problematic, as
|
||||
seen in the following example::
|
||||
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
pragma solidity >=0.6.0 <0.8.0;
|
||||
pragma solidity >0.6.99 <0.8.0;
|
||||
|
||||
contract owned {
|
||||
constructor() public { owner = msg.sender; }
|
||||
constructor() { owner = msg.sender; }
|
||||
address payable owner;
|
||||
}
|
||||
|
||||
@@ -157,10 +157,10 @@ explicitly in the final override, but this function will bypass
|
||||
``Base1.destroy``. The way around this is to use ``super``::
|
||||
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
pragma solidity >=0.6.0 <0.8.0;
|
||||
pragma solidity >0.6.99 <0.8.0;
|
||||
|
||||
contract owned {
|
||||
constructor() public { owner = msg.sender; }
|
||||
constructor() { owner = msg.sender; }
|
||||
address payable owner;
|
||||
}
|
||||
|
||||
@@ -392,33 +392,39 @@ and all functions that are reachable from there through function calls.
|
||||
It does not include the constructor code or internal functions that are
|
||||
only called from the constructor.
|
||||
|
||||
Constructor functions can be either ``public`` or ``internal``. If there is no
|
||||
If there is no
|
||||
constructor, the contract will assume the default constructor, which is
|
||||
equivalent to ``constructor() public {}``. For example:
|
||||
equivalent to ``constructor() {}``. For example:
|
||||
|
||||
::
|
||||
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
pragma solidity >=0.5.0 <0.8.0;
|
||||
pragma solidity >0.6.99 <0.8.0;
|
||||
|
||||
contract A {
|
||||
abstract contract A {
|
||||
uint public a;
|
||||
|
||||
constructor(uint _a) internal {
|
||||
constructor(uint _a) {
|
||||
a = _a;
|
||||
}
|
||||
}
|
||||
|
||||
contract B is A(1) {
|
||||
constructor() public {}
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
A constructor set as ``internal`` causes the contract to be marked as :ref:`abstract <abstract-contract>`.
|
||||
You can use internal parameters in a constructor (for example storage pointers). In this case,
|
||||
the contract has to be marked :ref:`abstract <abstract-contract>`, because these parameters
|
||||
cannot be assigned valid values from outside but only through the constructors of derived contracts.
|
||||
|
||||
.. warning ::
|
||||
Prior to version 0.4.22, constructors were defined as functions with the same name as the contract.
|
||||
This syntax was deprecated and is not allowed anymore in version 0.5.0.
|
||||
|
||||
.. warning ::
|
||||
Prior to version 0.7.0, you had to specify the visibility of constructors as either
|
||||
``internal`` or ``public``.
|
||||
|
||||
|
||||
.. index:: ! base;constructor
|
||||
|
||||
@@ -430,21 +436,21 @@ linearization rules explained below. If the base constructors have arguments,
|
||||
derived contracts need to specify all of them. This can be done in two ways::
|
||||
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
pragma solidity >=0.4.22 <0.8.0;
|
||||
pragma solidity >0.6.99 <0.8.0;
|
||||
|
||||
contract Base {
|
||||
uint x;
|
||||
constructor(uint _x) public { x = _x; }
|
||||
constructor(uint _x) { x = _x; }
|
||||
}
|
||||
|
||||
// Either directly specify in the inheritance list...
|
||||
contract Derived1 is Base(7) {
|
||||
constructor() public {}
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
// or through a "modifier" of the derived constructor.
|
||||
contract Derived2 is Base {
|
||||
constructor(uint _y) Base(_y * _y) public {}
|
||||
constructor(uint _y) Base(_y * _y) {}
|
||||
}
|
||||
|
||||
One way is directly in the inheritance list (``is Base(7)``). The other is in
|
||||
@@ -511,14 +517,14 @@ One area where inheritance linearization is especially important and perhaps not
|
||||
::
|
||||
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
pragma solidity >=0.4.22 <0.8.0;
|
||||
pragma solidity >0.6.99 <0.8.0;
|
||||
|
||||
contract Base1 {
|
||||
constructor() public {}
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
contract Base2 {
|
||||
constructor() public {}
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
// Constructors are executed in the following order:
|
||||
@@ -526,7 +532,7 @@ One area where inheritance linearization is especially important and perhaps not
|
||||
// 2 - Base2
|
||||
// 3 - Derived1
|
||||
contract Derived1 is Base1, Base2 {
|
||||
constructor() public Base1() Base2() {}
|
||||
constructor() Base1() Base2() {}
|
||||
}
|
||||
|
||||
// Constructors are executed in the following order:
|
||||
@@ -534,7 +540,7 @@ One area where inheritance linearization is especially important and perhaps not
|
||||
// 2 - Base1
|
||||
// 3 - Derived2
|
||||
contract Derived2 is Base2, Base1 {
|
||||
constructor() public Base2() Base1() {}
|
||||
constructor() Base2() Base1() {}
|
||||
}
|
||||
|
||||
// Constructors are still executed in the following order:
|
||||
@@ -542,7 +548,7 @@ One area where inheritance linearization is especially important and perhaps not
|
||||
// 2 - Base1
|
||||
// 3 - Derived3
|
||||
contract Derived3 is Base2, Base1 {
|
||||
constructor() public Base1() Base2() {}
|
||||
constructor() Base1() Base2() {}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user