Update Infofeed example code and explanation

This commit is contained in:
Chris Ward 2018-08-22 14:28:43 +02:00
parent 410d288dfc
commit 1475cde288

View File

@ -135,23 +135,18 @@ the gas can be specified with special options ``.value()`` and ``.gas()``, respe
contract Consumer { contract Consumer {
InfoFeed feed; InfoFeed feed;
function setFeed(address addr) public { feed = InfoFeed(addr); } function setFeed(InfoFeed addr) public { feed = addr; }
function callFeed() public { feed.info.value(10).gas(800)(); } function callFeed() public { feed.info.value(10).gas(800)(); }
} }
The modifier ``payable`` has to be used for ``info``, because otherwise, the `.value()` You need to use the modifier ``payable`` with the ``info`` function because
option would not be available. otherwise, the ``.value()`` option would not be available.
Note that the expression ``InfoFeed(addr)`` performs an explicit type conversion stating You can also use ``function setFeed(InfoFeed _feed) { feed = _feed; }``
that "we know that the type of the contract at the given address is ``InfoFeed``" and directly.
this does not execute a constructor. Explicit type conversions have to be
handled with extreme caution. Never call a function on a contract where you
are not sure about its type.
We could also have used ``function setFeed(InfoFeed _feed) { feed = _feed; }`` directly. .. warning::
Be careful about the fact that ``feed.info.value(10).gas(800)`` Be careful that ``feed.info.value(10).gas(800)`` only locally sets the ``value`` and amount of ``gas`` sent with the function call, and the parentheses at the end perform the actual call.
only (locally) sets the value and amount of gas sent with the function call and only the
parentheses at the end perform the actual call.
Function calls cause exceptions if the called contract does not exist (in the Function calls cause exceptions if the called contract does not exist (in the
sense that the account does not contain code) or if the called contract itself sense that the account does not contain code) or if the called contract itself