Merge pull request #6875 from ethereum/docs-style-fixes-values

[DOCS] Bring value types code examples inline with style guide
This commit is contained in:
Chris Chinchilla 2019-06-04 12:32:49 +03:00 committed by GitHub
commit 95e6b2e40d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -624,10 +624,12 @@ Example that shows how to use the members::
pragma solidity >=0.4.16 <0.7.0;
contract Example {
function f() public payable returns (bytes4) {
return this.f.selector;
}
function g() public {
this.f.gas(10).value(800)();
}
@ -637,6 +639,7 @@ Example that shows how to use internal function types::
pragma solidity >=0.4.16 <0.7.0;
library ArrayUtils {
// internal functions can be used in internal library functions because
// they will be part of the same code context
@ -650,6 +653,7 @@ Example that shows how to use internal function types::
r[i] = f(self[i]);
}
}
function reduce(
uint[] memory self,
function (uint, uint) pure returns (uint) f
@ -663,6 +667,7 @@ Example that shows how to use internal function types::
r = f(r, self[i]);
}
}
function range(uint length) internal pure returns (uint[] memory r) {
r = new uint[](length);
for (uint i = 0; i < r.length; i++) {
@ -671,14 +676,18 @@ Example that shows how to use internal function types::
}
}
contract Pyramid {
using ArrayUtils for *;
function pyramid(uint l) public pure returns (uint) {
return ArrayUtils.range(l).map(square).reduce(sum);
}
function square(uint x) internal pure returns (uint) {
return x * x;
}
function sum(uint x, uint y) internal pure returns (uint) {
return x + y;
}
@ -688,32 +697,39 @@ Another example that uses external function types::
pragma solidity >=0.4.22 <0.7.0;
contract Oracle {
struct Request {
bytes data;
function(uint) external callback;
}
Request[] requests;
Request[] private requests;
event NewRequest(uint);
function query(bytes memory data, function(uint) external callback) public {
requests.push(Request(data, callback));
emit NewRequest(requests.length - 1);
}
function reply(uint requestID, uint response) public {
// Here goes the check that the reply comes from a trusted source
requests[requestID].callback(response);
}
}
contract OracleUser {
Oracle constant oracle = Oracle(0x1234567); // known contract
uint exchangeRate;
Oracle constant private ORACLE_CONST = Oracle(0x1234567); // known contract
uint private exchangeRate;
function buySomething() public {
oracle.query("USD", this.oracleResponse);
ORACLE_CONST.query("USD", this.oracleResponse);
}
function oracleResponse(uint response) public {
require(
msg.sender == address(oracle),
msg.sender == address(ORACLE_CONST),
"Only oracle can call this."
);
exchangeRate = response;