mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Update libraries section for 0.6.0.
This commit is contained in:
parent
b9c35916e5
commit
bbb8b88118
@ -36,12 +36,12 @@ if the library were a base contract. Of course, calls to internal functions
|
|||||||
use the internal calling convention, which means that all internal types
|
use the internal calling convention, which means that all internal types
|
||||||
can be passed and types :ref:`stored in memory <data-location>` will be passed by reference and not copied.
|
can be passed and types :ref:`stored in memory <data-location>` will be passed by reference and not copied.
|
||||||
To realize this in the EVM, code of internal library functions
|
To realize this in the EVM, code of internal library functions
|
||||||
and all functions called from therein will at compile time be pulled into the calling
|
and all functions called from therein will at compile time be included in the calling
|
||||||
contract, and a regular ``JUMP`` call will be used instead of a ``DELEGATECALL``.
|
contract, and a regular ``JUMP`` call will be used instead of a ``DELEGATECALL``.
|
||||||
|
|
||||||
.. index:: using for, set
|
.. index:: using for, set
|
||||||
|
|
||||||
The following example illustrates how to use libraries (but manual method
|
The following example illustrates how to use libraries (but using a manual method,
|
||||||
be sure to check out :ref:`using for <using-for>` for a
|
be sure to check out :ref:`using for <using-for>` for a
|
||||||
more advanced example to implement a set).
|
more advanced example to implement a set).
|
||||||
|
|
||||||
@ -50,11 +50,11 @@ more advanced example to implement a set).
|
|||||||
pragma solidity >=0.4.22 <0.7.0;
|
pragma solidity >=0.4.22 <0.7.0;
|
||||||
|
|
||||||
|
|
||||||
library Set {
|
|
||||||
// We define a new struct datatype that will be used to
|
// We define a new struct datatype that will be used to
|
||||||
// hold its data in the calling contract.
|
// hold its data in the calling contract.
|
||||||
struct Data { mapping(uint => bool) flags; }
|
struct Data { mapping(uint => bool) flags; }
|
||||||
|
|
||||||
|
library Set {
|
||||||
// Note that the first parameter is of type "storage
|
// Note that the first parameter is of type "storage
|
||||||
// reference" and thus only its storage address and not
|
// reference" and thus only its storage address and not
|
||||||
// its contents is passed as part of the call. This is a
|
// its contents is passed as part of the call. This is a
|
||||||
@ -92,7 +92,7 @@ more advanced example to implement a set).
|
|||||||
|
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
Set.Data knownValues;
|
Data knownValues;
|
||||||
|
|
||||||
function register(uint value) public {
|
function register(uint value) public {
|
||||||
// The library functions can be called without a
|
// The library functions can be called without a
|
||||||
@ -125,11 +125,11 @@ custom types without the overhead of external function calls:
|
|||||||
|
|
||||||
pragma solidity >=0.4.16 <0.7.0;
|
pragma solidity >=0.4.16 <0.7.0;
|
||||||
|
|
||||||
library BigInt {
|
|
||||||
struct bigint {
|
struct bigint {
|
||||||
uint[] limbs;
|
uint[] limbs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
library BigInt {
|
||||||
function fromUint(uint x) internal pure returns (bigint memory r) {
|
function fromUint(uint x) internal pure returns (bigint memory r) {
|
||||||
r.limbs = new uint[](1);
|
r.limbs = new uint[](1);
|
||||||
r.limbs[0] = x;
|
r.limbs[0] = x;
|
||||||
@ -168,12 +168,12 @@ custom types without the overhead of external function calls:
|
|||||||
}
|
}
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
using BigInt for BigInt.bigint;
|
using BigInt for bigint;
|
||||||
|
|
||||||
function f() public pure {
|
function f() public pure {
|
||||||
BigInt.bigint memory x = BigInt.fromUint(7);
|
bigint memory x = BigInt.fromUint(7);
|
||||||
BigInt.bigint memory y = BigInt.fromUint(uint(-1));
|
bigint memory y = BigInt.fromUint(uint(-1));
|
||||||
BigInt.bigint memory z = x.add(y);
|
bigint memory z = x.add(y);
|
||||||
assert(z.limb(1) > 0);
|
assert(z.limb(1) > 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -194,17 +194,18 @@ encoding of the address of the library contract.
|
|||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
Manually linking libraries on the generated bytecode is discouraged, because
|
Manually linking libraries on the generated bytecode is discouraged, because
|
||||||
it is restricted to 36 characters.
|
in this way, the library name is restricted to 36 characters.
|
||||||
You should ask the compiler to link the libraries at the time
|
You should ask the compiler to link the libraries at the time
|
||||||
a contract is compiled by either using
|
a contract is compiled by either using
|
||||||
the ``--libraries`` option of ``solc`` or the ``libraries`` key if you use
|
the ``--libraries`` option of ``solc`` or the ``libraries`` key if you use
|
||||||
the standard-JSON interface to the compiler.
|
the standard-JSON interface to the compiler.
|
||||||
|
|
||||||
Restrictions for libraries in comparison to contracts:
|
In comparison to contracts, libraries are restricted in the following ways:
|
||||||
|
|
||||||
- No state variables
|
- they cannot have state variables
|
||||||
- Cannot inherit nor be inherited
|
- they cannot inherit nor be inherited
|
||||||
- Cannot receive Ether
|
- they cannot receive Ether
|
||||||
|
- they cannot be destroyed
|
||||||
|
|
||||||
(These might be lifted at a later point.)
|
(These might be lifted at a later point.)
|
||||||
|
|
||||||
@ -275,3 +276,7 @@ this causes the deploy time address to be the first
|
|||||||
constant to be pushed onto the stack and the dispatcher
|
constant to be pushed onto the stack and the dispatcher
|
||||||
code compares the current address against this constant
|
code compares the current address against this constant
|
||||||
for any non-view and non-pure function.
|
for any non-view and non-pure function.
|
||||||
|
|
||||||
|
This means that the actual code stored on chain for a library
|
||||||
|
is different from the code reported by the compiler as
|
||||||
|
``deployedBytecode``.
|
Loading…
Reference in New Issue
Block a user