Bring value types code examples inline with style guide

This commit is contained in:
Chris Chinchilla 2019-05-30 14:37:17 +02:00
parent dd04a35c0e
commit 69e4e69e69

View File

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