Merge pull request #7981 from ethereum/updateUsingFor

Update "using for".
This commit is contained in:
chriseth 2019-12-12 12:44:19 +01:00 committed by GitHub
commit 700d5e6103
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,8 @@ Using For
*********
The directive ``using A for B;`` can be used to attach library
functions (from the library ``A``) to any type (``B``).
functions (from the library ``A``) to any type (``B``)
in the context of a contract.
These functions will receive the object they are called on
as their first parameter (like the ``self`` variable in Python).
@ -25,9 +26,6 @@ contract, including within all of its functions, and has no effect
outside of the contract in which it is used. The directive
may only be used inside a contract, not inside any of its functions.
By including a library, its data types including library functions are
available without having to add further code.
Let us rewrite the set example from the
:ref:`libraries` in this way::
@ -35,9 +33,9 @@ Let us rewrite the set example from the
// This is the same code as before, just without comments
library Set {
struct Data { mapping(uint => bool) flags; }
struct Data { mapping(uint => bool) flags; }
library Set {
function insert(Data storage self, uint value)
public
returns (bool)
@ -69,11 +67,11 @@ Let us rewrite the set example from the
contract C {
using Set for Set.Data; // this is the crucial change
Set.Data knownValues;
using Set for Data; // this is the crucial change
Data knownValues;
function register(uint value) public {
// Here, all variables of type Set.Data have
// Here, all variables of type Data have
// corresponding member functions.
// The following function call is identical to
// `Set.insert(knownValues, value)`
@ -115,7 +113,8 @@ It is also possible to extend elementary types in that way::
}
}
Note that all library calls are actual EVM function calls. This means that
Note that all external library calls are actual EVM function calls. This means that
if you pass memory or value types, a copy will be performed, even of the
``self`` variable. The only situation where no copy will be performed
is when storage reference variables are used.
is when storage reference variables are used or when internal library
functions are called.