From ee51894e733be6c1bf73b51140c3bf2a08343825 Mon Sep 17 00:00:00 2001 From: Mircea Moca Date: Thu, 18 Aug 2016 14:16:01 +0300 Subject: Update contracts.rst Extended example according to the discussed stuff, clarification & typos --- docs/contracts.rst | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'docs') diff --git a/docs/contracts.rst b/docs/contracts.rst index 5f370951..c38fdb9d 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. @@ -194,9 +194,16 @@ return parameter list for functions. function setData(uint a) internal { data = a; } uint public data; } + contract Caller { + function readData(){ + C c = new C(); + uint local = c.data(); // call accessor + local = f(7); // error + } + } 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 +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). .. index:: ! accessor;function, ! function;accessor @@ -205,16 +212,16 @@ 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. 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. :: -- cgit From 01dc055d424ad79d11fc27f007a9b6893ffbf714 Mon Sep 17 00:00:00 2001 From: Mircea Moca Date: Fri, 19 Aug 2016 17:47:57 +0300 Subject: Update contracts.rst I think it's more clear now and without the risk of mentioning the yet discussed accessor concept --- docs/contracts.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs') diff --git a/docs/contracts.rst b/docs/contracts.rst index c38fdb9d..3ee98937 100644 --- a/docs/contracts.rst +++ b/docs/contracts.rst @@ -194,16 +194,16 @@ return parameter list for functions. function setData(uint a) internal { data = a; } uint public data; } - contract Caller { + contract D { function readData(){ C c = new C(); - uint local = c.data(); // call accessor + uint local = c.data(); local = f(7); // error } } -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 +An other contract ``D`` can call ``c.data()`` to retrieve the value of data in state +storage, is not able to call ``f``. Contracts derived from ``C`` can call ``setData`` to alter the value of ``data`` (but only in their own state). .. index:: ! accessor;function, ! function;accessor -- cgit From 55be44dd5fbd4dc704e5ea1f6fdadd297a62a11e Mon Sep 17 00:00:00 2001 From: Mircea Moca Date: Thu, 25 Aug 2016 11:43:17 +0300 Subject: Update contracts.rst I left the example with contract C only, showing the access specifiers discussed at 186-188. The specifier of data is left public since nothing is related to accessor functions now. Added a separate example with contracts C, D and E to show specifier's effect. Added a separate example to prove the synthesizing of accessor functions. Added a new example to show the internal/external call of the accessor, according to existent discussion. --- docs/contracts.rst | 56 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) (limited to 'docs') diff --git a/docs/contracts.rst b/docs/contracts.rst index 3ee98937..4372c278 100644 --- a/docs/contracts.rst +++ b/docs/contracts.rst @@ -194,17 +194,38 @@ return parameter list for functions. function setData(uint a) internal { data = a; } uint public data; } + +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(); - uint local = c.data(); - local = f(7); // error + 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) } } -An other contract ``D`` can call ``c.data()`` to retrieve the value of data in state -storage, is not able to call ``f``. Contracts derived from ``C`` can call -``setData`` to alter the value of ``data`` (but only in their own state). .. index:: ! accessor;function, ! function;accessor @@ -218,6 +239,19 @@ 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 evaluated as state variable and if it is accessed externally @@ -225,8 +259,12 @@ it is evaluated as state variable and if it is accessed externally :: - 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: -- cgit