From 42a84946d2dca1880082a07d6408f5b88b966f9c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 21 Nov 2016 13:11:43 +0000 Subject: [PATCH 1/3] Fix function type examples --- docs/types.rst | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/types.rst b/docs/types.rst index b22ad7d4c..693e20953 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -314,10 +314,13 @@ followed by the function identifier together in a single ``bytes24`` type. Example that shows how to use internal function types:: + pragma solidity ^0.4.5; + library ArrayUtils { // internal functions can be used in internal library functions because // they will be part of the same code context function map(uint[] memory self, function (uint) returns (uint) f) + internal returns (uint[] memory r) { r = new uint[](self.length); @@ -327,8 +330,9 @@ Example that shows how to use internal function types:: } function reduce( uint[] memory self, - function (uint) returns (uint) f + function (uint x, uint y) returns (uint) f ) + internal returns (uint r) { r = self[0]; @@ -336,7 +340,7 @@ Example that shows how to use internal function types:: 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); for (uint i = 0; i < r.length; i++) { r[i] = i; @@ -346,7 +350,7 @@ Example that shows how to use internal function types:: contract Pyramid { using ArrayUtils for *; - function pyramid(uint l) return (uint) { + function pyramid(uint l) returns (uint) { return ArrayUtils.range(l).map(square).reduce(sum); } function square(uint x) internal returns (uint) { @@ -359,6 +363,8 @@ Example that shows how to use internal function types:: Another example that uses external function types:: + pragma solidity ^0.4.5; + contract Oracle { struct Request { bytes data; @@ -377,12 +383,12 @@ Another example that uses external function types:: } contract OracleUser { - Oracle constant oracle = 0x1234567; // known contract + Oracle constant oracle = Oracle(0x1234567); // known contract function buySomething() { oracle.query("USD", oracleResponse); } function oracleResponse(bytes response) { - if (msg.sender != oracle) throw; + if (msg.sender != address(oracle)) throw; // Use the data } } From 81eea45c2df6c29cd6e8c2258b10d44b9c058962 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 21 Nov 2016 13:16:35 +0000 Subject: [PATCH 2/3] Fix oracle callback example --- docs/types.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/types.rst b/docs/types.rst index 693e20953..9f3b4dc1b 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -368,11 +368,11 @@ Another example that uses external function types:: contract Oracle { struct Request { bytes data; - function(bytes) external callback; + function(bytes memory) external callback; } Request[] requests; 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)); NewRequest(requests.length - 1); } From 73eb0235b4bb9673cf653ca834893e33f7410381 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 21 Nov 2016 17:09:08 +0100 Subject: [PATCH 3/3] Fix examples and add explanation. --- docs/types.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/types.rst b/docs/types.rst index 9f3b4dc1b..1db35ced5 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -312,6 +312,10 @@ If external function types are used outside of the context of Solidity, they are treated as the ``function`` type, which encodes the address 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:: pragma solidity ^0.4.5; @@ -385,7 +389,7 @@ Another example that uses external function types:: contract OracleUser { Oracle constant oracle = Oracle(0x1234567); // known contract function buySomething() { - oracle.query("USD", oracleResponse); + oracle.query("USD", this.oracleResponse); } function oracleResponse(bytes response) { if (msg.sender != address(oracle)) throw;