Merge pull request #4402 from ethereum/docsConstructor

Update docs to new constructor syntax
This commit is contained in:
chriseth 2018-07-02 18:29:51 +02:00 committed by GitHub
commit f5e1cf7753
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 40 deletions

View File

@ -437,10 +437,10 @@ For example,
:: ::
pragma solidity ^0.4.0; pragma solidity >0.4.24;
contract Test { contract Test {
function Test() public { b = 0x12345678901234567890123456789012; } constructor() public { b = 0x12345678901234567890123456789012; }
event Event(uint indexed a, bytes32 b); event Event(uint indexed a, bytes32 b);
event Event2(uint indexed a, bytes32 b); event Event2(uint indexed a, bytes32 b);
function foo(uint a) public { emit Event(a, b); } function foo(uint a) public { emit Event(a, b); }
@ -582,4 +582,4 @@ Note that constants will be packed using the minimum number of bytes required to
This means that, for example, ``abi.encodePacked(0) == abi.encodePacked(uint8(0)) == hex"00"`` and This means that, for example, ``abi.encodePacked(0) == abi.encodePacked(uint8(0)) == hex"00"`` and
``abi.encodePacked(0x12345678) == abi.encodePacked(uint32(0x12345678)) == hex"12345678"``. ``abi.encodePacked(0x12345678) == abi.encodePacked(uint32(0x12345678)) == hex"12345678"``.
If padding is needed, explicit type conversions can be used: ``abi.encodePacked(uint16(0x12)) == hex"0012"``. If padding is needed, explicit type conversions can be used: ``abi.encodePacked(uint16(0x12)) == hex"0012"``.

View File

@ -28,7 +28,7 @@ become the new richest.
:: ::
pragma solidity ^0.4.11; pragma solidity >0.4.24;
contract WithdrawalContract { contract WithdrawalContract {
address public richest; address public richest;
@ -36,7 +36,7 @@ become the new richest.
mapping (address => uint) pendingWithdrawals; mapping (address => uint) pendingWithdrawals;
function WithdrawalContract() public payable { constructor() public payable {
richest = msg.sender; richest = msg.sender;
mostSent = msg.value; mostSent = msg.value;
} }
@ -65,13 +65,13 @@ This is as opposed to the more intuitive sending pattern:
:: ::
pragma solidity ^0.4.11; pragma solidity >0.4.24;
contract SendContract { contract SendContract {
address public richest; address public richest;
uint public mostSent; uint public mostSent;
function SendContract() public payable { constructor() public payable {
richest = msg.sender; richest = msg.sender;
mostSent = msg.value; mostSent = msg.value;
} }

View File

@ -301,10 +301,10 @@ inheritable properties of contracts and may be overridden by derived contracts.
:: ::
pragma solidity ^0.4.22; pragma solidity >0.4.24;
contract owned { contract owned {
function owned() public { owner = msg.sender; } constructor() public { owner = msg.sender; }
address owner; address owner;
// This contract only defines a modifier but does not use // This contract only defines a modifier but does not use
@ -346,7 +346,7 @@ inheritable properties of contracts and may be overridden by derived contracts.
mapping (address => bool) registeredAddresses; mapping (address => bool) registeredAddresses;
uint price; uint price;
function Register(uint initialPrice) public { price = initialPrice; } constructor(uint initialPrice) public { price = initialPrice; }
// It is important to also provide the // It is important to also provide the
// `payable` keyword here, otherwise the function will // `payable` keyword here, otherwise the function will
@ -990,7 +990,7 @@ default constructor: ``contructor() public {}``.
:: ::
pragma solidity ^0.4.22; pragma solidity >0.4.24;
contract A { contract A {
uint public a; uint public a;
@ -1006,24 +1006,8 @@ default constructor: ``contructor() public {}``.
A constructor set as ``internal`` causes the contract to be marked as :ref:`abstract <abstract-contract>`. A constructor set as ``internal`` causes the contract to be marked as :ref:`abstract <abstract-contract>`.
.. note :: .. warning ::
Prior to version 0.4.22, constructors were defined as functions with the same name as the contract. This syntax is now deprecated. 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.
::
pragma solidity ^0.4.11;
contract A {
uint public a;
function A(uint _a) internal {
a = _a;
}
}
contract B is A(1) {
function B() public {}
}
.. index:: ! base;constructor .. index:: ! base;constructor
@ -1402,7 +1386,7 @@ Using For
********* *********
The directive ``using A for B;`` can be used to attach library The directive ``using A for B;`` can be used to attach library
functions (from the library ``A``) to any type (``B``). functions (from the library ``A``) to any type (``B``).
These functions will receive the object they are called on These functions will receive the object they are called on
as their first parameter (like the ``self`` variable in Python). as their first parameter (like the ``self`` variable in Python).

View File

@ -225,11 +225,11 @@ creation-dependencies are not possible.
:: ::
pragma solidity ^0.4.0; pragma solidity >0.4.24;
contract D { contract D {
uint x; uint x;
function D(uint a) public payable { constructor(uint a) public payable {
x = a; x = a;
} }
} }

View File

@ -426,10 +426,10 @@ In the case of a ``contract A`` calling a new instance of ``contract B``, parent
You will need to make sure that you have both contracts aware of each other's presence and that ``contract B`` has a ``payable`` constructor. You will need to make sure that you have both contracts aware of each other's presence and that ``contract B`` has a ``payable`` constructor.
In this example:: In this example::
pragma solidity ^0.4.0; pragma solidity >0.4.24;
contract B { contract B {
function B() public payable {} constructor() public payable {}
} }
contract A { contract A {

View File

@ -80,7 +80,7 @@ registering with username and password — all you need is an Ethereum keypair.
:: ::
pragma solidity ^0.4.21; pragma solidity >0.4.24;
contract Coin { contract Coin {
// The keyword "public" makes those variables // The keyword "public" makes those variables
@ -94,7 +94,7 @@ registering with username and password — all you need is an Ethereum keypair.
// This is the constructor whose code is // This is the constructor whose code is
// run only when the contract is created. // run only when the contract is created.
function Coin() public { constructor() public {
minter = msg.sender; minter = msg.sender;
} }

View File

@ -180,13 +180,13 @@ Never use tx.origin for authorization. Let's say you have a wallet contract like
:: ::
pragma solidity ^0.4.11; pragma solidity >0.4.24;
// THIS CONTRACT CONTAINS A BUG - DO NOT USE // THIS CONTRACT CONTAINS A BUG - DO NOT USE
contract TxUserWallet { contract TxUserWallet {
address owner; address owner;
function TxUserWallet() public { constructor() public {
owner = msg.sender; owner = msg.sender;
} }
@ -200,7 +200,7 @@ Now someone tricks you into sending ether to the address of this attack wallet:
:: ::
pragma solidity ^0.4.11; pragma solidity >0.4.24;
interface TxUserWallet { interface TxUserWallet {
function transferTo(address dest, uint amount) public; function transferTo(address dest, uint amount) public;
@ -209,7 +209,7 @@ Now someone tricks you into sending ether to the address of this attack wallet:
contract TxAttackWallet { contract TxAttackWallet {
address owner; address owner;
function TxAttackWallet() public { constructor() public {
owner = msg.sender; owner = msg.sender;
} }