Update "using for".

This commit is contained in:
chriseth 2019-12-12 10:49:03 +01:00
parent b9c35916e5
commit f4a0546db3

View File

@ -7,7 +7,8 @@ Using For
********* *********
The directive ``using A for B;`` can be used to attach library 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 These functions will receive the object they are called on
as their first parameter (like the ``self`` variable in Python). 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 outside of the contract in which it is used. The directive
may only be used inside a contract, not inside any of its functions. 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 Let us rewrite the set example from the
:ref:`libraries` in this way:: :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 // 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) function insert(Data storage self, uint value)
public public
returns (bool) returns (bool)
@ -69,11 +67,11 @@ Let us rewrite the set example from the
contract C { contract C {
using Set for Set.Data; // this is the crucial change using Set for Data; // this is the crucial change
Set.Data knownValues; Data knownValues;
function register(uint value) public { function register(uint value) public {
// Here, all variables of type Set.Data have // Here, all variables of type Data have
// corresponding member functions. // corresponding member functions.
// The following function call is identical to // The following function call is identical to
// `Set.insert(knownValues, value)` // `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 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 ``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.