mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #1411 from ethereum/function-type-docs
Fix function type examples
This commit is contained in:
commit
475009b93d
@ -312,12 +312,19 @@ If external function types are used outside of the context of Solidity,
|
|||||||
they are treated as the ``function`` type, which encodes the address
|
they are treated as the ``function`` type, which encodes the address
|
||||||
followed by the function identifier together in a single ``bytes24`` type.
|
followed by the function identifier together in a single ``bytes24`` type.
|
||||||
|
|
||||||
|
Note that public functions of the current contract can be used both as an
|
||||||
|
internal and as an external function. To use ``f`` as an internal function,
|
||||||
|
just use ``f``, if you want to use its external form, use ``this.f``.
|
||||||
|
|
||||||
Example that shows how to use internal function types::
|
Example that shows how to use internal function types::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.5;
|
||||||
|
|
||||||
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) returns (uint) f)
|
function map(uint[] memory self, function (uint) returns (uint) f)
|
||||||
|
internal
|
||||||
returns (uint[] memory r)
|
returns (uint[] memory r)
|
||||||
{
|
{
|
||||||
r = new uint[](self.length);
|
r = new uint[](self.length);
|
||||||
@ -327,8 +334,9 @@ Example that shows how to use internal function types::
|
|||||||
}
|
}
|
||||||
function reduce(
|
function reduce(
|
||||||
uint[] memory self,
|
uint[] memory self,
|
||||||
function (uint) returns (uint) f
|
function (uint x, uint y) returns (uint) f
|
||||||
)
|
)
|
||||||
|
internal
|
||||||
returns (uint r)
|
returns (uint r)
|
||||||
{
|
{
|
||||||
r = self[0];
|
r = self[0];
|
||||||
@ -336,7 +344,7 @@ Example that shows how to use internal function types::
|
|||||||
r = f(r, self[i]);
|
r = f(r, self[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function range(uint length) returns (uint[] memory r) {
|
function range(uint length) internal 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;
|
||||||
@ -346,7 +354,7 @@ Example that shows how to use internal function types::
|
|||||||
|
|
||||||
contract Pyramid {
|
contract Pyramid {
|
||||||
using ArrayUtils for *;
|
using ArrayUtils for *;
|
||||||
function pyramid(uint l) return (uint) {
|
function pyramid(uint l) returns (uint) {
|
||||||
return ArrayUtils.range(l).map(square).reduce(sum);
|
return ArrayUtils.range(l).map(square).reduce(sum);
|
||||||
}
|
}
|
||||||
function square(uint x) internal returns (uint) {
|
function square(uint x) internal returns (uint) {
|
||||||
@ -359,14 +367,16 @@ Example that shows how to use internal function types::
|
|||||||
|
|
||||||
Another example that uses external function types::
|
Another example that uses external function types::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.5;
|
||||||
|
|
||||||
contract Oracle {
|
contract Oracle {
|
||||||
struct Request {
|
struct Request {
|
||||||
bytes data;
|
bytes data;
|
||||||
function(bytes) external callback;
|
function(bytes memory) external callback;
|
||||||
}
|
}
|
||||||
Request[] requests;
|
Request[] requests;
|
||||||
event NewRequest(uint);
|
event NewRequest(uint);
|
||||||
function query(bytes data, function(bytes) external callback) {
|
function query(bytes data, function(bytes memory) external callback) {
|
||||||
requests.push(Request(data, callback));
|
requests.push(Request(data, callback));
|
||||||
NewRequest(requests.length - 1);
|
NewRequest(requests.length - 1);
|
||||||
}
|
}
|
||||||
@ -377,12 +387,12 @@ Another example that uses external function types::
|
|||||||
}
|
}
|
||||||
|
|
||||||
contract OracleUser {
|
contract OracleUser {
|
||||||
Oracle constant oracle = 0x1234567; // known contract
|
Oracle constant oracle = Oracle(0x1234567); // known contract
|
||||||
function buySomething() {
|
function buySomething() {
|
||||||
oracle.query("USD", oracleResponse);
|
oracle.query("USD", this.oracleResponse);
|
||||||
}
|
}
|
||||||
function oracleResponse(bytes response) {
|
function oracleResponse(bytes response) {
|
||||||
if (msg.sender != oracle) throw;
|
if (msg.sender != address(oracle)) throw;
|
||||||
// Use the data
|
// Use the data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user