diff options
author | chriseth <chris@ethereum.org> | 2016-08-26 00:06:30 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-26 00:06:30 +0800 |
commit | f329d5e772ed4314897c46b67b1459f39838a2cc (patch) | |
tree | 1ac666d1767a8a063683ea2397b9c022793f378a /docs/contracts.rst | |
parent | c2665dc2fe0e7e973ae599ac7010fa6939ebd20f (diff) | |
parent | 55be44dd5fbd4dc704e5ea1f6fdadd297a62a11e (diff) | |
download | dexon-solidity-f329d5e772ed4314897c46b67b1459f39838a2cc.tar.gz dexon-solidity-f329d5e772ed4314897c46b67b1459f39838a2cc.tar.zst dexon-solidity-f329d5e772ed4314897c46b67b1459f39838a2cc.zip |
Merge pull request #925 from mocamircea/patch-10
Update contracts.rst
Diffstat (limited to 'docs/contracts.rst')
-rw-r--r-- | docs/contracts.rst | 67 |
1 files changed, 56 insertions, 11 deletions
diff --git a/docs/contracts.rst b/docs/contracts.rst index 911bfc0d..e9fc4526 100644 --- a/docs/contracts.rst +++ b/docs/contracts.rst @@ -179,7 +179,7 @@ and the default is ``internal``. .. note:: Everything that is inside a contract is visible to all external observers. Making something ``private`` - only prevents other contract from accessing and modifying + only prevents other contracts from accessing and modifying the information, but it will still be visible to the whole world outside of the blockchain. @@ -195,9 +195,37 @@ return parameter list for functions. uint public data; } -Other contracts can call ``c.data()`` to retrieve the value of data in state -storage, but are not able to call ``f``. Contracts derived from ``c`` can call -``setData`` to alter the value of ``data`` (but only in their own state). +An other contract ``D`` can call ``c.getData()`` to retrieve the value of data in state +storage and is not able to call ``f``. Contract ``E`` is derived from ``C`` and can call +``compute``. + +:: + + contract C { + function f(uint a) private returns(uint b) { return a + 1; } + function setData(uint a) { data = a; } + function getData() public returns(uint) {return data;} + function compute(uint a, uint b) internal returns (uint) { return a+b;} + uint private data; + } + + contract D { + function readData() { + C c = new C(); + local = c.f(7); // error: member "f" is not visible + c.setData(3); + uint local = c.getData(); + local = c.compute(3,5); // error: member "compute" is not visible + } + } + + contract E is C { + function g() { + C c = new C(); + uint val = compute(3,5); // acces to internal member (from derivated to parent contract) + } + } + .. index:: ! accessor;function, ! function;accessor @@ -205,21 +233,38 @@ Accessor Functions ================== The compiler automatically creates accessor functions for -all public state variables. The contract given below will -have a function called ``data`` that does not take any -arguments and returns a uint, the value of the state +all public state variables. For the contract given below the compiler will +generate a function called ``data`` that does not take any +arguments and returns a ``uint``, the value of the state variable ``data``. The initialization of state variables can be done at declaration. +:: + + contract C { + uint public data = 42; + } + + contract Caller { + C c = new C(); + function f() { + uint local = c.data(); + } + } + The accessor functions have external visibility. If the symbol is accessed internally (i.e. without ``this.``), -it is a state variable and if it is accessed externally -(i.e. with ``this.``), it is a function. +it is evaluated as state variable and if it is accessed externally +(i.e. with ``this.``), it is evaluated as function. :: - contract Test { - uint public data = 42; + contract C { + uint public data; + function x() { + data = 3; // internal access + uint val = this.data(); // external access + } } The next example is a bit more complex: |