mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Update documentation.
This commit is contained in:
parent
6a5a187d83
commit
8a06000a30
@ -137,16 +137,16 @@ Functions have to be specified as being ``external``,
|
|||||||
For state variables, ``external`` is not possible.
|
For state variables, ``external`` is not possible.
|
||||||
|
|
||||||
``external``:
|
``external``:
|
||||||
External functions are part of the contract
|
External functions are part of the contract interface,
|
||||||
interface, which means they can be called from other contracts and
|
which means they can be called from other contracts and
|
||||||
via transactions. An external function ``f`` cannot be called
|
via transactions. An external function ``f`` cannot be called
|
||||||
internally (i.e. ``f()`` does not work, but ``this.f()`` works).
|
internally (i.e. ``f()`` does not work, but ``this.f()`` works).
|
||||||
External functions are sometimes more efficient when
|
External functions are sometimes more efficient when
|
||||||
they receive large arrays of data.
|
they receive large arrays of data.
|
||||||
|
|
||||||
``public``:
|
``public``:
|
||||||
Public functions are part of the contract
|
Public functions are part of the contract interface
|
||||||
interface and can be either called internally or via
|
and can be either called internally or via
|
||||||
messages. For public state variables, an automatic getter
|
messages. For public state variables, an automatic getter
|
||||||
function (see below) is generated.
|
function (see below) is generated.
|
||||||
|
|
||||||
|
@ -159,8 +159,8 @@ throws an exception or goes out of gas.
|
|||||||
|
|
||||||
.. warning::
|
.. warning::
|
||||||
Any interaction with another contract imposes a potential danger, especially
|
Any interaction with another contract imposes a potential danger, especially
|
||||||
if the source code of the contract is not known in advance. The current
|
if the source code of the contract is not known in advance. The
|
||||||
contract hands over control to the called contract and that may potentially
|
current contract hands over control to the called contract and that may potentially
|
||||||
do just about anything. Even if the called contract inherits from a known parent contract,
|
do just about anything. Even if the called contract inherits from a known parent contract,
|
||||||
the inheriting contract is only required to have a correct interface. The
|
the inheriting contract is only required to have a correct interface. The
|
||||||
implementation of the contract, however, can be completely arbitrary and thus,
|
implementation of the contract, however, can be completely arbitrary and thus,
|
||||||
@ -293,7 +293,7 @@ These can then either be assigned to newly declared variables or to pre-existing
|
|||||||
|
|
||||||
function g() public {
|
function g() public {
|
||||||
// Variables declared with type and assigned from the returned tuple,
|
// Variables declared with type and assigned from the returned tuple,
|
||||||
// not all elements have to be specified (but the amount must match).
|
// not all elements have to be specified (but the number must match).
|
||||||
(uint x, , uint y) = f();
|
(uint x, , uint y) = f();
|
||||||
// Common trick to swap values -- does not work for non-value storage types.
|
// Common trick to swap values -- does not work for non-value storage types.
|
||||||
(x, y) = (y, x);
|
(x, y) = (y, x);
|
||||||
|
@ -52,31 +52,35 @@ Surround top level declarations in solidity source with two blank lines.
|
|||||||
|
|
||||||
Yes::
|
Yes::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract A {
|
contract A {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract B {
|
contract B {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
No::
|
No::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract A {
|
contract A {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
contract B {
|
contract B {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
Within a contract surround function declarations with a single blank line.
|
Within a contract surround function declarations with a single blank line.
|
||||||
@ -85,30 +89,34 @@ Blank lines may be omitted between groups of related one-liners (such as stub fu
|
|||||||
|
|
||||||
Yes::
|
Yes::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract A {
|
contract A {
|
||||||
function spam() public;
|
function spam() public pure;
|
||||||
function ham() public;
|
function ham() public pure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract B is A {
|
contract B is A {
|
||||||
function spam() public {
|
function spam() public pure {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
function ham() public {
|
function ham() public pure {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
No::
|
No::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract A {
|
contract A {
|
||||||
function spam() public {
|
function spam() public pure {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
function ham() public {
|
function ham() public pure {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,22 +237,24 @@ Import statements should always be placed at the top of the file.
|
|||||||
|
|
||||||
Yes::
|
Yes::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
import "./Owned.sol";
|
import "./Owned.sol";
|
||||||
|
|
||||||
|
|
||||||
contract A {
|
contract A {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract B is Owned {
|
contract B is Owned {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
No::
|
No::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract A {
|
contract A {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -252,7 +262,7 @@ No::
|
|||||||
|
|
||||||
|
|
||||||
contract B is Owned {
|
contract B is Owned {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
Order of Functions
|
Order of Functions
|
||||||
@ -273,13 +283,15 @@ Within a grouping, place the ``view`` and ``pure`` functions last.
|
|||||||
|
|
||||||
Yes::
|
Yes::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract A {
|
contract A {
|
||||||
constructor() public {
|
constructor() public {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
function() external {
|
function() external {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
// External functions
|
// External functions
|
||||||
@ -303,13 +315,15 @@ Yes::
|
|||||||
|
|
||||||
No::
|
No::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract A {
|
contract A {
|
||||||
|
|
||||||
// External functions
|
// External functions
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
function() external {
|
function() external {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private functions
|
// Private functions
|
||||||
@ -319,7 +333,7 @@ No::
|
|||||||
// ...
|
// ...
|
||||||
|
|
||||||
constructor() public {
|
constructor() public {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal functions
|
// Internal functions
|
||||||
@ -397,6 +411,8 @@ should:
|
|||||||
|
|
||||||
Yes::
|
Yes::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract Coin {
|
contract Coin {
|
||||||
struct Bank {
|
struct Bank {
|
||||||
address owner;
|
address owner;
|
||||||
@ -406,6 +422,8 @@ Yes::
|
|||||||
|
|
||||||
No::
|
No::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract Coin
|
contract Coin
|
||||||
{
|
{
|
||||||
struct Bank {
|
struct Bank {
|
||||||
@ -705,7 +723,25 @@ manner as modifiers if the function declaration is long or hard to read.
|
|||||||
|
|
||||||
Yes::
|
Yes::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
|
// Base contracts just to make this compile
|
||||||
|
contract B {
|
||||||
|
constructor(uint) public {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
contract C {
|
||||||
|
constructor(uint, uint) public {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
contract D {
|
||||||
|
constructor(uint) public {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
contract A is B, C, D {
|
contract A is B, C, D {
|
||||||
|
uint x;
|
||||||
|
|
||||||
constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
|
constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
|
||||||
B(param1)
|
B(param1)
|
||||||
C(param2, param3)
|
C(param2, param3)
|
||||||
@ -713,29 +749,50 @@ Yes::
|
|||||||
public
|
public
|
||||||
{
|
{
|
||||||
// do something with param5
|
// do something with param5
|
||||||
|
x = param5;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
No::
|
No::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
|
// Base contracts just to make this compile
|
||||||
|
contract B {
|
||||||
|
constructor(uint) public {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
contract C {
|
||||||
|
constructor(uint, uint) public {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
contract D {
|
||||||
|
constructor(uint) public {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
contract A is B, C, D {
|
contract A is B, C, D {
|
||||||
|
uint x;
|
||||||
|
|
||||||
constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
|
constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
|
||||||
B(param1)
|
B(param1)
|
||||||
C(param2, param3)
|
C(param2, param3)
|
||||||
D(param4)
|
D(param4)
|
||||||
public
|
public
|
||||||
{
|
{
|
||||||
// do something with param5
|
x = param5;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contract A is B, C, D {
|
contract X is B, C, D {
|
||||||
|
uint x;
|
||||||
|
|
||||||
constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
|
constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
|
||||||
B(param1)
|
B(param1)
|
||||||
C(param2, param3)
|
C(param2, param3)
|
||||||
D(param4)
|
D(param4)
|
||||||
public {
|
public {
|
||||||
// do something with param5
|
x = param5;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -875,6 +932,8 @@ As shown in the example below, if the contract name is `Congress` and the librar
|
|||||||
|
|
||||||
Yes::
|
Yes::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
// Owned.sol
|
// Owned.sol
|
||||||
contract Owned {
|
contract Owned {
|
||||||
address public owner;
|
address public owner;
|
||||||
@ -897,11 +956,13 @@ Yes::
|
|||||||
import "./Owned.sol";
|
import "./Owned.sol";
|
||||||
|
|
||||||
contract Congress is Owned, TokenRecipient {
|
contract Congress is Owned, TokenRecipient {
|
||||||
...
|
//...
|
||||||
}
|
}
|
||||||
|
|
||||||
No::
|
No::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
// owned.sol
|
// owned.sol
|
||||||
contract owned {
|
contract owned {
|
||||||
address public owner;
|
address public owner;
|
||||||
@ -924,7 +985,7 @@ No::
|
|||||||
import "./owned.sol";
|
import "./owned.sol";
|
||||||
|
|
||||||
contract Congress is owned, tokenRecipient {
|
contract Congress is owned, tokenRecipient {
|
||||||
...
|
//...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -662,7 +662,7 @@ Allocating Memory Arrays
|
|||||||
Creating arrays with variable length in memory can be done using the ``new`` keyword.
|
Creating arrays with variable length in memory can be done using the ``new`` keyword.
|
||||||
As opposed to storage arrays, it is **not** possible to resize memory arrays (e.g. by assigning to
|
As opposed to storage arrays, it is **not** possible to resize memory arrays (e.g. by assigning to
|
||||||
the ``.length`` member). You either have to calculate the required size in advance
|
the ``.length`` member). You either have to calculate the required size in advance
|
||||||
or crete a new memory array and copy every element.
|
or create a new memory array and copy every element.
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user