diff options
-rw-r--r-- | Changelog.md | 2 | ||||
-rw-r--r-- | docs/types.rst | 26 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 11 |
3 files changed, 30 insertions, 9 deletions
diff --git a/Changelog.md b/Changelog.md index eb1e5e72..468518d2 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,7 +1,7 @@ ### 0.4.6 (2016-11-22) Bugfixes: - * Optimizer: Knowledge about state was not correctly cleared for JUMPDESTs + * Optimizer: Knowledge about state was not correctly cleared for JUMPDESTs (introduced in 0.4.5) ### 0.4.5 (2016-11-21) diff --git a/docs/types.rst b/docs/types.rst index b22ad7d4..1db35ced 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -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 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; + 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 +334,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 +344,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 +354,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,14 +367,16 @@ 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; - 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); } @@ -377,12 +387,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); + oracle.query("USD", this.oracleResponse); } function oracleResponse(bytes response) { - if (msg.sender != oracle) throw; + if (msg.sender != address(oracle)) throw; // Use the data } } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 7cacbf8f..b0e0bbb4 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2817,6 +2817,17 @@ BOOST_AUTO_TEST_CASE(using_for_not_used) BOOST_CHECK(expectError(text) == Error::Type::TypeError); } +BOOST_AUTO_TEST_CASE(library_memory_struct) +{ + char const* text = R"( + library c { + struct S { uint x; } + function f() returns (S ) {} + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + BOOST_AUTO_TEST_CASE(using_for_arbitrary_mismatch) { // Bound to a, but self type does not match. |