solidity/docs/types.rst

1010 lines
39 KiB
ReStructuredText
Raw Normal View History

2015-12-07 20:16:25 +00:00
.. index:: type
.. _types:
2015-12-07 20:16:25 +00:00
*****
Types
*****
Solidity is a statically typed language, which means that the type of each
variable (state and local) needs to be specified (or at least known -
see :ref:`type-deduction` below) at
compile-time. Solidity provides several elementary types which can be combined
2016-06-18 04:05:38 +00:00
to form complex types.
2015-12-07 20:16:25 +00:00
2016-07-20 19:35:38 +00:00
In addition, types can interact with each other in expressions containing
operators. For a quick reference of the various operators, see :ref:`order`.
2015-12-07 20:16:25 +00:00
.. index:: ! value type, ! type;value
Value Types
===========
The following types are also called value types because variables of these
types will always be passed by value, i.e. they are always copied when they
are used as function arguments or in assignments.
.. index:: ! bool, ! true, ! false
Booleans
--------
``bool``: The possible values are constants ``true`` and ``false``.
2015-12-07 20:16:25 +00:00
2016-05-13 14:32:35 +00:00
Operators:
2015-12-07 20:16:25 +00:00
* ``!`` (logical negation)
* ``&&`` (logical conjunction, "and")
* ``||`` (logical disjunction, "or")
* ``==`` (equality)
* ``!=`` (inequality)
2015-12-07 20:16:25 +00:00
The operators ``||`` and ``&&`` apply the common short-circuiting rules. This means that in the expression ``f(x) || g(y)``, if ``f(x)`` evaluates to ``true``, ``g(y)`` will not be evaluated even if it may have side-effects.
2015-12-07 20:16:25 +00:00
.. index:: ! uint, ! int, ! integer
Integers
--------
``int`` / ``uint``: Signed and unsigned integers of various sizes. Keywords ``uint8`` to ``uint256`` in steps of ``8`` (unsigned of 8 up to 256 bits) and ``int8`` to ``int256``. ``uint`` and ``int`` are aliases for ``uint256`` and ``int256``, respectively.
2015-12-07 20:16:25 +00:00
2016-05-13 14:32:35 +00:00
Operators:
2015-12-07 20:16:25 +00:00
* Comparisons: ``<=``, ``<``, ``==``, ``!=``, ``>=``, ``>`` (evaluate to ``bool``)
2016-12-13 15:43:16 +00:00
* Bit operators: ``&``, ``|``, ``^`` (bitwise exclusive or), ``~`` (bitwise negation)
* Arithmetic operators: ``+``, ``-``, unary ``-``, unary ``+``, ``*``, ``/``, ``%`` (remainder), ``**`` (exponentiation), ``<<`` (left shift), ``>>`` (right shift)
2015-12-07 20:16:25 +00:00
2017-08-28 10:48:23 +00:00
Division always truncates (it is just compiled to the ``DIV`` opcode of the EVM), but it does not truncate if both
2016-05-18 20:53:36 +00:00
operators are :ref:`literals<rational_literals>` (or literal expressions).
2016-12-13 15:43:16 +00:00
Division by zero and modulus with zero throws a runtime exception.
2016-08-12 14:50:37 +00:00
2016-12-09 19:18:37 +00:00
The result of a shift operation is the type of the left operand. The
expression ``x << y`` is equivalent to ``x * 2**y``, and ``x >> y`` is
2016-12-09 19:18:37 +00:00
equivalent to ``x / 2**y``. This means that shifting negative numbers
2016-12-13 15:43:16 +00:00
sign extends. Shifting by a negative amount throws a runtime exception.
2016-12-09 19:18:37 +00:00
2017-04-28 12:00:59 +00:00
.. warning::
The results produced by shift right of negative values of signed integer types is different from those produced
by other programming languages. In Solidity, shift right maps to division so the shifted negative values
are going to be rounded towards zero (truncated). In other programming languages the shift right of negative values
works like division with rounding down (towards negative infinity).
2017-08-29 20:26:39 +00:00
.. index:: ! ufixed, ! fixed, ! fixed point number
Fixed Point Numbers
-------------------
.. warning::
Fixed point numbers are not fully supported by Solidity yet. They can be declared, but
cannot be assigned to or from.
2017-12-12 18:47:30 +00:00
``fixed`` / ``ufixed``: Signed and unsigned fixed point number of various sizes. Keywords ``ufixedMxN`` and ``fixedMxN``, where ``M`` represents the number of bits taken by
the type and ``N`` represents how many decimal points are available. ``M`` must be divisible by 8 and goes from 8 to 256 bits. ``N`` must be between 0 and 80, inclusive.
``ufixed`` and ``fixed`` are aliases for ``ufixed128x18`` and ``fixed128x18``, respectively.
2017-08-29 20:38:41 +00:00
Operators:
* Comparisons: ``<=``, ``<``, ``==``, ``!=``, ``>=``, ``>`` (evaluate to ``bool``)
* Arithmetic operators: ``+``, ``-``, unary ``-``, unary ``+``, ``*``, ``/``, ``%`` (remainder)
.. note::
The main difference between floating point (``float`` and ``double`` in many languages, more precisely IEEE 754 numbers) and fixed point numbers is
that the number of bits used for the integer and the fractional part (the part after the decimal dot) is flexible in the former, while it is strictly
defined in the latter. Generally, in floating point almost the entire space is used to represent the number, while only a small number of bits define
where the decimal point is.
2017-02-08 23:37:23 +00:00
.. index:: address, balance, send, call, callcode, delegatecall, transfer
2015-12-07 20:16:25 +00:00
2016-05-18 19:37:38 +00:00
.. _address:
2015-12-07 20:16:25 +00:00
Address
-------
``address``: Holds a 20 byte value (size of an Ethereum address). Address types also have members and serve as a base for all contracts.
2015-12-07 20:16:25 +00:00
2016-05-13 14:32:35 +00:00
Operators:
2015-12-07 20:16:25 +00:00
* ``<=``, ``<``, ``==``, ``!=``, ``>=`` and ``>``
2015-12-07 20:16:25 +00:00
.. note::
Starting with version 0.5.0 contracts do not derive from the address type, but can still be explicitly converted to address.
2017-07-28 05:07:18 +00:00
.. _members-of-addresses:
2015-12-07 20:16:25 +00:00
Members of Addresses
^^^^^^^^^^^^^^^^^^^^
* ``balance`` and ``transfer``
2015-12-07 20:16:25 +00:00
2016-06-02 13:29:53 +00:00
For a quick reference, see :ref:`address_related`.
It is possible to query the balance of an address using the property ``balance``
and to send Ether (in units of wei) to an address using the ``transfer`` function:
2015-12-07 20:16:25 +00:00
::
address x = 0x123;
address myAddress = this;
if (x.balance < 10 && myAddress.balance >= 10) x.transfer(10);
2015-12-07 20:16:25 +00:00
.. note::
If ``x`` is a contract address, its code (more specifically: its fallback function, if present) will be executed together with the ``transfer`` call (this is a feature of the EVM and cannot be prevented). If that execution runs out of gas or fails in any way, the Ether transfer will be reverted and the current contract will stop with an exception.
* ``send``
Send is the low-level counterpart of ``transfer``. If the execution fails, the current contract will not stop with an exception, but ``send`` will return ``false``.
2015-12-07 20:16:25 +00:00
.. warning::
2016-06-02 13:14:53 +00:00
There are some dangers in using ``send``: The transfer fails if the call stack depth is at 1024
(this can always be forced by the caller) and it also fails if the recipient runs out of gas. So in order
to make safe Ether transfers, always check the return value of ``send``, use ``transfer`` or even better:
use a pattern where the recipient withdraws the money.
2017-02-08 23:37:23 +00:00
* ``call``, ``callcode`` and ``delegatecall``
2015-12-07 20:16:25 +00:00
Furthermore, to interface with contracts that do not adhere to the ABI,
the function ``call`` is provided which takes an arbitrary number of arguments of any type. These arguments are padded to 32 bytes and concatenated. One exception is the case where the first argument is encoded to exactly four bytes. In this case, it is not padded to allow the use of function signatures here.
2015-12-07 20:16:25 +00:00
::
address nameReg = 0x72ba7d8e73fe8eb666ea66babc8116a41bfb10e2;
nameReg.call("register", "MyName");
nameReg.call(bytes4(keccak256("fun(uint256)")), a);
2015-12-07 20:16:25 +00:00
2016-06-01 17:15:17 +00:00
``call`` returns a boolean indicating whether the invoked function terminated (``true``) or caused an EVM exception (``false``). It is not possible to access the actual data returned (for this we would need to know the encoding and size in advance).
2015-12-07 20:16:25 +00:00
2017-08-29 21:48:54 +00:00
It is possible to adjust the supplied gas with the ``.gas()`` modifier::
namReg.call.gas(1000000)("register", "MyName");
Similarly, the supplied Ether value can be controlled too::
nameReg.call.value(1 ether)("register", "MyName");
Lastly, these modifiers can be combined. Their order does not matter::
nameReg.call.gas(1000000).value(1 ether)("register", "MyName");
.. note::
It is not yet possible to use the gas or value modifiers on overloaded functions.
A workaround is to introduce a special case for gas and value and just re-check
whether they are present at the point of overload resolution.
In a similar way, the function ``delegatecall`` can be used: the difference is that only the code of the given address is used, all other aspects (storage, balance, ...) are taken from the current contract. The purpose of ``delegatecall`` is to use library code which is stored in another contract. The user has to ensure that the layout of storage in both contracts is suitable for delegatecall to be used. Prior to homestead, only a limited variant called ``callcode`` was available that did not provide access to the original ``msg.sender`` and ``msg.value`` values.
2015-12-07 20:16:25 +00:00
All three functions ``call``, ``delegatecall`` and ``callcode`` are very low-level functions and should only be used as a *last resort* as they break the type-safety of Solidity.
2015-12-07 20:16:25 +00:00
The ``.gas()`` option is available on all three methods, while the ``.value()`` option is not supported for ``delegatecall``.
2015-12-07 20:16:25 +00:00
.. note::
All contracts can be converted to ``address`` type, so it is possible to query the balance of the
current contract using ``address(this).balance``.
2015-12-07 20:16:25 +00:00
2017-07-26 11:20:38 +00:00
.. note::
The use of ``callcode`` is discouraged and will be removed in the future.
2016-06-28 15:29:08 +00:00
.. warning::
All these functions are low-level functions and should be used with care.
Specifically, any unknown contract might be malicious and if you call it, you
hand over control to that contract which could in turn call back into
your contract, so be prepared for changes to your state variables
when the call returns.
2015-12-07 20:16:25 +00:00
.. index:: byte array, bytes32
Fixed-size byte arrays
----------------------
``bytes1``, ``bytes2``, ``bytes3``, ..., ``bytes32``. ``byte`` is an alias for ``bytes1``.
2015-12-07 20:16:25 +00:00
2016-05-13 14:32:35 +00:00
Operators:
2015-12-07 20:16:25 +00:00
* Comparisons: ``<=``, ``<``, ``==``, ``!=``, ``>=``, ``>`` (evaluate to ``bool``)
2016-12-09 19:18:37 +00:00
* Bit operators: ``&``, ``|``, ``^`` (bitwise exclusive or), ``~`` (bitwise negation), ``<<`` (left shift), ``>>`` (right shift)
* Index access: If ``x`` is of type ``bytesI``, then ``x[k]`` for ``0 <= k < I`` returns the ``k`` th byte (read-only).
2016-02-03 20:42:38 +00:00
2016-12-13 15:43:16 +00:00
The shifting operator works with any integer type as right operand (but will
return the type of the left operand), which denotes the number of bits to shift by.
Shifting by a negative amount will cause a runtime exception.
2016-12-09 19:18:37 +00:00
2016-02-03 20:42:38 +00:00
Members:
* ``.length`` yields the fixed length of the byte array (read-only).
2015-12-07 20:16:25 +00:00
2017-08-29 21:53:50 +00:00
.. note::
It is possible to use an array of bytes as ``byte[]``, but it is wasting a lot of space, 31 bytes every element,
to be exact, when passing in calls. It is better to use ``bytes``.
2017-01-24 22:10:01 +00:00
.. index:: address, literal;address
.. _address_literals:
Address Literals
----------------
Hexadecimal literals that pass the address checksum test, for example
``0xdCad3a6d3569DF655070DEd06cb7A1b2Ccd1D3AF`` are of ``address`` type.
2017-01-26 12:56:50 +00:00
Hexadecimal literals that are between 39 and 41 digits
long and do not pass the checksum test produce
2017-01-24 22:10:01 +00:00
a warning and are treated as regular rational number literals.
.. note::
The mixed-case address checksum format is defined in `EIP-55 <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md>`_.
2016-05-18 20:53:36 +00:00
.. index:: literal, literal;rational
2015-12-07 20:16:25 +00:00
2016-05-18 20:53:36 +00:00
.. _rational_literals:
Rational and Integer Literals
-----------------------------
Integer literals are formed from a sequence of numbers in the range 0-9.
They are interpreted as decimals. For example, ``69`` means sixty nine.
2017-03-07 11:34:32 +00:00
Octal literals do not exist in Solidity and leading zeros are invalid.
2017-03-07 11:34:32 +00:00
Decimal fraction literals are formed by a ``.`` with at least one number on
one side. Examples include ``1.``, ``.1`` and ``1.3``.
Scientific notation is also supported, where the base can have fractions, while the exponent cannot.
Examples include ``2e10``, ``-2e10``, ``2e-10``, ``2.5e1``.
2016-11-23 14:18:46 +00:00
Number literal expressions retain arbitrary precision until they are converted to a non-literal type (i.e. by
using them together with a non-literal expression).
This means that computations do not overflow and divisions do not truncate
in number literal expressions.
2016-05-18 20:53:36 +00:00
For example, ``(2**800 + 1) - 2**800`` results in the constant ``1`` (of type ``uint8``)
although intermediate results would not even fit the machine word size. Furthermore, ``.5 * 8`` results
in the integer ``4`` (although non-integers were used in between).
2016-05-18 20:53:36 +00:00
2016-11-23 14:18:46 +00:00
Any operator that can be applied to integers can also be applied to number literal expressions as
2016-10-04 19:06:13 +00:00
long as the operands are integers. If any of the two is fractional, bit operations are disallowed
2016-05-18 20:53:36 +00:00
and exponentiation is disallowed if the exponent is fractional (because that might result in
a non-rational number).
2016-11-25 13:51:38 +00:00
.. note::
Solidity has a number literal type for each rational number.
Integer literals and rational number literals belong to number literal types.
Moreover, all number literal expressions (i.e. the expressions that
contain only number literals and operators) belong to number literal
types. So the number literal expressions ``1 + 2`` and ``2 + 1`` both
2016-11-25 13:51:38 +00:00
belong to the same number literal type for the rational number three.
.. warning::
Division on integer literals used to truncate in earlier versions, but it will now convert into a rational number, i.e. ``5 / 2`` is not equal to ``2``, but to ``2.5``.
2016-05-18 20:53:36 +00:00
.. note::
2016-11-23 14:18:46 +00:00
Number literal expressions are converted into a non-literal type as soon as they are used with non-literal
2016-05-18 20:53:36 +00:00
expressions. Even though we know that the value of the
expression assigned to ``b`` in the following example evaluates to
an integer, but the partial expression ``2.5 + a`` does not type check so the code
2016-05-18 20:53:36 +00:00
does not compile
::
2016-05-18 20:53:36 +00:00
uint128 a = 1;
uint128 b = 2.5 + a + 0.5;
2015-12-07 20:16:25 +00:00
.. index:: literal, literal;string, string
String Literals
---------------
String literals are written with either double or single-quotes (``"foo"`` or ``'bar'``). They do not imply trailing zeroes as in C; ``"foo"`` represents three bytes not four. As with integer literals, their type can vary, but they are implicitly convertible to ``bytes1``, ..., ``bytes32``, if they fit, to ``bytes`` and to ``string``.
2015-12-07 20:16:25 +00:00
2016-08-10 15:31:20 +00:00
String literals support escape characters, such as ``\n``, ``\xNN`` and ``\uNNNN``. ``\xNN`` takes a hex value and inserts the appropriate byte, while ``\uNNNN`` takes a Unicode codepoint and inserts an UTF-8 sequence.
2016-08-04 16:35:00 +00:00
.. index:: literal, bytes
Hexadecimal Literals
--------------------
Hexademical Literals are prefixed with the keyword ``hex`` and are enclosed in double or single-quotes (``hex"001122FF"``). Their content must be a hexadecimal string and their value will be the binary representation of those values.
Hexademical Literals behave like String Literals and have the same convertibility restrictions.
2015-12-07 20:16:25 +00:00
.. index:: enum
.. _enums:
2015-12-07 20:16:25 +00:00
Enums
2016-10-19 16:43:07 +00:00
-----
2015-12-07 20:16:25 +00:00
Enums are one way to create a user-defined type in Solidity. They are explicitly convertible
to and from all integer types but implicit conversion is not allowed. The explicit conversions
check the value ranges at runtime and a failure causes an exception. Enums needs at least one member.
2015-12-07 20:16:25 +00:00
::
2017-12-12 18:47:30 +00:00
pragma solidity ^0.4.16;
2015-12-07 20:16:25 +00:00
contract test {
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
ActionChoices choice;
ActionChoices constant defaultChoice = ActionChoices.GoStraight;
2016-05-18 15:05:28 +00:00
2017-12-12 18:47:30 +00:00
function setGoStraight() public {
2015-12-07 20:16:25 +00:00
choice = ActionChoices.GoStraight;
}
2016-05-18 15:05:28 +00:00
2015-12-07 20:16:25 +00:00
// Since enum types are not part of the ABI, the signature of "getChoice"
// will automatically be changed to "getChoice() returns (uint8)"
// for all matters external to Solidity. The integer type used is just
// large enough to hold all enum values, i.e. if you have more values,
// `uint16` will be used and so on.
2017-12-12 18:47:30 +00:00
function getChoice() public view returns (ActionChoices) {
2015-12-07 20:16:25 +00:00
return choice;
}
2016-05-18 15:05:28 +00:00
2017-12-12 18:47:30 +00:00
function getDefaultChoice() public pure returns (uint) {
2015-12-07 20:16:25 +00:00
return uint(defaultChoice);
}
}
2016-10-19 16:43:07 +00:00
.. index:: ! function type, ! type; function
.. _function_types:
Function Types
--------------
2016-11-15 10:43:46 +00:00
Function types are the types of functions. Variables of function type
can be assigned from functions and function parameters of function type
can be used to pass functions to and return functions from function calls.
Function types come in two flavours - *internal* and *external* functions:
2016-10-19 16:43:07 +00:00
Internal functions can only be called inside the current contract (more specifically,
2016-11-08 10:43:42 +00:00
inside the current code unit, which also includes internal library functions
and inherited functions) because they cannot be executed outside of the
context of the current contract. Calling an internal function is realized
by jumping to its entry label, just like when calling a function of the current
2016-10-19 16:43:07 +00:00
contract internally.
External functions consist of an address and a function signature and they can
be passed via and returned from external function calls.
2016-11-15 10:43:46 +00:00
Function types are notated as follows::
2016-10-19 16:43:07 +00:00
2017-08-16 19:53:46 +00:00
function (<parameter types>) {internal|external} [pure|constant|view|payable] [returns (<return types>)]
2016-10-19 16:43:07 +00:00
2016-11-08 10:43:42 +00:00
In contrast to the parameter types, the return types cannot be empty - if the
function type should not return anything, the whole ``returns (<return types>)``
part has to be omitted.
2016-10-19 16:43:07 +00:00
By default, function types are internal, so the ``internal`` keyword can be
omitted. In contrast, contract functions themselves are public by default,
only when used as the name of a type, the default is internal.
2016-10-19 16:43:07 +00:00
There are two ways to access a function in the current contract: Either directly
by its name, ``f``, or using ``this.f``. The former will result in an internal
function, the latter in an external function.
2016-10-19 16:43:07 +00:00
If a function type variable is not initialized, calling it will result
in an exception. The same happens if you call a function after using ``delete``
on it.
2016-10-19 16:43:07 +00:00
If external function types are used outside of the context of Solidity,
2016-11-14 22:54:44 +00:00
they are treated as the ``function`` type, which encodes the address
followed by the function identifier together in a single ``bytes24`` type.
2016-10-19 16:43:07 +00:00
2016-11-21 16:09:08 +00:00
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``.
2017-07-11 17:10:42 +00:00
Additionally, public (or external) functions also have a special member called ``selector``,
which returns the :ref:`ABI function selector <abi_function_selector>`::
2017-12-12 18:47:30 +00:00
pragma solidity ^0.4.16;
2017-07-11 17:10:42 +00:00
contract Selector {
2017-12-12 18:47:30 +00:00
function f() public view returns (bytes4) {
2017-07-11 17:10:42 +00:00
return this.f.selector;
}
}
2016-11-14 20:51:41 +00:00
Example that shows how to use internal function types::
2016-10-19 16:43:07 +00:00
2017-12-12 18:47:30 +00:00
pragma solidity ^0.4.16;
2016-11-21 13:11:43 +00:00
2016-10-19 16:43:07 +00:00
library ArrayUtils {
2016-10-26 16:01:08 +00:00
// internal functions can be used in internal library functions because
// they will be part of the same code context
2017-12-12 18:47:30 +00:00
function map(uint[] memory self, function (uint) pure returns (uint) f)
2016-11-21 13:11:43 +00:00
internal
2017-12-12 18:47:30 +00:00
pure
2016-10-26 16:01:08 +00:00
returns (uint[] memory r)
{
r = new uint[](self.length);
for (uint i = 0; i < self.length; i++) {
r[i] = f(self[i]);
}
}
function reduce(
uint[] memory self,
2017-12-12 18:47:30 +00:00
function (uint, uint) pure returns (uint) f
2016-10-26 16:01:08 +00:00
)
2016-11-21 13:11:43 +00:00
internal
2017-12-12 18:47:30 +00:00
pure
2017-07-10 22:07:27 +00:00
returns (uint r)
2016-10-26 16:01:08 +00:00
{
r = self[0];
for (uint i = 1; i < self.length; i++) {
r = f(r, self[i]);
}
}
2017-12-12 18:47:30 +00:00
function range(uint length) internal pure returns (uint[] memory r) {
2016-10-26 16:01:08 +00:00
r = new uint[](length);
for (uint i = 0; i < r.length; i++) {
r[i] = i;
}
}
}
2017-12-12 18:47:30 +00:00
2016-10-26 16:01:08 +00:00
contract Pyramid {
using ArrayUtils for *;
2017-12-12 18:47:30 +00:00
function pyramid(uint l) public pure returns (uint) {
2016-10-26 16:01:08 +00:00
return ArrayUtils.range(l).map(square).reduce(sum);
}
2017-12-12 18:47:30 +00:00
function square(uint x) internal pure returns (uint) {
2016-10-26 16:01:08 +00:00
return x * x;
}
2017-12-12 18:47:30 +00:00
function sum(uint x, uint y) internal pure returns (uint) {
2016-10-26 16:01:08 +00:00
return x + y;
}
}
2016-11-14 20:51:41 +00:00
Another example that uses external function types::
2016-10-26 16:01:08 +00:00
pragma solidity ^0.4.22;
2016-11-21 13:11:43 +00:00
2016-10-26 16:01:08 +00:00
contract Oracle {
struct Request {
bytes data;
2016-11-21 13:16:35 +00:00
function(bytes memory) external callback;
2016-10-26 16:01:08 +00:00
}
Request[] requests;
event NewRequest(uint);
2017-12-12 18:47:30 +00:00
function query(bytes data, function(bytes memory) external callback) public {
2016-10-26 16:01:08 +00:00
requests.push(Request(data, callback));
2018-02-16 16:32:30 +00:00
emit NewRequest(requests.length - 1);
2016-10-26 16:01:08 +00:00
}
2017-12-12 18:47:30 +00:00
function reply(uint requestID, bytes response) public {
2016-10-26 16:01:08 +00:00
// Here goes the check that the reply comes from a trusted source
requests[requestID].callback(response);
}
}
contract OracleUser {
2016-11-21 13:11:43 +00:00
Oracle constant oracle = Oracle(0x1234567); // known contract
2016-10-26 16:01:08 +00:00
function buySomething() {
2016-11-21 16:09:08 +00:00
oracle.query("USD", this.oracleResponse);
2016-10-26 16:01:08 +00:00
}
2017-12-12 18:47:30 +00:00
function oracleResponse(bytes response) public {
2018-01-03 14:30:01 +00:00
require(
msg.sender == address(oracle),
"Only oracle can call this."
);
2016-10-26 16:01:08 +00:00
// Use the data
}
}
2017-07-26 11:20:38 +00:00
.. note::
Lambda or inline functions are planned but not yet supported.
2016-10-19 16:43:07 +00:00
2015-12-07 20:16:25 +00:00
.. index:: ! type;reference, ! reference type, storage, memory, location, array, struct
Reference Types
==================
Complex types, i.e. types which do not always fit into 256 bits have to be handled
more carefully than the value-types we have already seen. Since copying
them can be quite expensive, we have to think about whether we want them to be
stored in **memory** (which is not persisting) or **storage** (where the state
variables are held).
Dynamically-sized byte array
----------------------------
``bytes``:
Dynamically-sized byte array, see :ref:`arrays`. Not a value-type!
``string``:
Dynamically-sized UTF-8-encoded string, see :ref:`arrays`. Not a value-type!
As a rule of thumb, use ``bytes`` for arbitrary-length raw byte data and ``string``
for arbitrary-length string (UTF-8) data. If you can limit the length to a certain
number of bytes, always use one of ``bytes1`` to ``bytes32`` because they are much cheaper.
2015-12-07 20:16:25 +00:00
Data location
-------------
Every complex type, i.e. *arrays* and *structs*, has an additional
annotation, the "data location", about whether it is stored in memory or in storage. Depending on the
context, there is always a default, but it can be overridden by appending
either ``storage`` or ``memory`` to the type. The default for function parameters (including return parameters) is ``memory``, the default for local variables is ``storage`` and the location is forced
to ``storage`` for state variables (obviously).
2015-12-07 20:16:25 +00:00
2017-08-28 10:48:23 +00:00
There is also a third data location, ``calldata``, which is a non-modifiable,
2015-12-07 20:16:25 +00:00
non-persistent area where function arguments are stored. Function parameters
2017-08-28 10:48:23 +00:00
(not return parameters) of external functions are forced to ``calldata`` and
behave mostly like ``memory``.
2015-12-07 20:16:25 +00:00
Data locations are important because they change how assignments behave:
assignments between storage and memory and also to a state variable (even from other state variables)
2015-12-07 20:16:25 +00:00
always create an independent copy.
Assignments to local storage variables only assign a reference though, and
this reference always points to the state variable even if the latter is changed
in the meantime.
On the other hand, assignments from a memory stored reference type to another
memory-stored reference type do not create a copy.
2015-12-07 20:16:25 +00:00
::
pragma solidity ^0.4.0;
2016-05-18 15:05:28 +00:00
contract C {
uint[] x; // the data location of x is storage
// the data location of memoryArray is memory
2017-12-12 18:47:30 +00:00
function f(uint[] memoryArray) public {
2016-05-18 15:05:28 +00:00
x = memoryArray; // works, copies the whole array to storage
var y = x; // works, assigns a pointer, data location of y is storage
y[7]; // fine, returns the 8th element
y.length = 2; // fine, modifies x through y
delete x; // fine, clears the array, also modifies y
// The following does not work; it would need to create a new temporary /
// unnamed array in storage, but storage is "statically" allocated:
// y = memoryArray;
// This does not work either, since it would "reset" the pointer, but there
// is no sensible location it could point to.
// delete y;
g(x); // calls g, handing over a reference to x
h(x); // calls h and creates an independent, temporary copy in memory
}
function g(uint[] storage storageArray) internal {}
2017-12-12 18:47:30 +00:00
function h(uint[] memoryArray) public {}
2015-12-07 20:16:25 +00:00
}
Summary
^^^^^^^
Forced data location:
- parameters (not return) of external functions: calldata
- state variables: storage
Default data location:
- parameters (also return) of functions: memory
- all other local variables: storage
.. index:: ! array
.. _arrays:
Arrays
------
Arrays can have a compile-time fixed size or they can be dynamic.
For storage arrays, the element type can be arbitrary (i.e. also other
arrays, mappings or structs). For memory arrays, it cannot be a mapping and
has to be an ABI type if it is an argument of a publicly-visible function.
An array of fixed size ``k`` and element type ``T`` is written as ``T[k]``,
an array of dynamic size as ``T[]``. As an example, an array of 5 dynamic
arrays of ``uint`` is ``uint[][5]`` (note that the notation is reversed when
2015-12-07 20:16:25 +00:00
compared to some other languages). To access the second uint in the
third dynamic array, you use ``x[2][1]`` (indices are zero-based and
access works in the opposite way of the declaration, i.e. ``x[2]``
2015-12-07 20:16:25 +00:00
shaves off one level in the type from the right).
Variables of type ``bytes`` and ``string`` are special arrays. A ``bytes`` is similar to ``byte[]``,
but it is packed tightly in calldata. ``string`` is equal to ``bytes`` but does not allow
2015-12-07 20:16:25 +00:00
length or index access (for now).
So ``bytes`` should always be preferred over ``byte[]`` because it is cheaper.
2015-12-07 22:31:58 +00:00
2015-12-07 20:16:25 +00:00
.. note::
If you want to access the byte-representation of a string ``s``, use
``bytes(s).length`` / ``bytes(s)[7] = 'x';``. Keep in mind
2016-08-10 15:27:09 +00:00
that you are accessing the low-level bytes of the UTF-8 representation,
2015-12-07 20:16:25 +00:00
and not the individual characters!
2017-07-01 03:31:31 +00:00
It is possible to mark arrays ``public`` and have Solidity create a :ref:`getter <visibility-and-getters>`.
2017-02-02 23:52:34 +00:00
The numeric index will become a required parameter for the getter.
2016-10-19 13:13:48 +00:00
2016-03-14 14:43:27 +00:00
.. index:: ! array;allocating, new
Allocating Memory Arrays
^^^^^^^^^^^^^^^^^^^^^^^^
Creating arrays with variable length in memory can be done using the ``new`` keyword.
2016-03-14 14:46:55 +00:00
As opposed to storage arrays, it is **not** possible to resize memory arrays by assigning to
the ``.length`` member.
2016-03-14 14:43:27 +00:00
::
2017-12-12 18:47:30 +00:00
pragma solidity ^0.4.16;
2016-03-14 14:43:27 +00:00
contract C {
2017-12-12 18:47:30 +00:00
function f(uint len) public pure {
2016-05-18 15:05:28 +00:00
uint[] memory a = new uint[](7);
bytes memory b = new bytes(len);
// Here we have a.length == 7 and b.length == len
a[6] = 8;
}
2016-03-14 14:43:27 +00:00
}
2016-05-31 13:55:58 +00:00
.. index:: ! array;literals, !inline;arrays
Array Literals / Inline Arrays
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Array literals are arrays that are written as an expression and are not
assigned to a variable right away.
::
2017-12-12 18:47:30 +00:00
pragma solidity ^0.4.16;
2016-05-31 13:55:58 +00:00
contract C {
2017-12-12 18:47:30 +00:00
function f() public pure {
2016-05-31 13:55:58 +00:00
g([uint(1), 2, 3]);
}
2017-12-13 07:55:46 +00:00
function g(uint[3] _data) public pure {
2016-05-31 13:55:58 +00:00
// ...
}
}
The type of an array literal is a memory array of fixed size whose base
type is the common type of the given elements. The type of ``[1, 2, 3]`` is
``uint8[3] memory``, because the type of each of these constants is ``uint8``.
Because of that, it was necessary to convert the first element in the example
above to ``uint``. Note that currently, fixed size memory arrays cannot
be assigned to dynamically-sized memory arrays, i.e. the following is not
possible:
::
2017-07-10 22:07:27 +00:00
// This will not compile.
pragma solidity ^0.4.0;
2016-05-31 13:55:58 +00:00
contract C {
2017-12-12 18:47:30 +00:00
function f() public {
2016-05-31 13:55:58 +00:00
// The next line creates a type error because uint[3] memory
// cannot be converted to uint[] memory.
2018-05-22 08:21:32 +00:00
uint[] memory x = [uint(1), 3, 4];
2017-07-10 22:07:27 +00:00
}
2016-05-31 13:55:58 +00:00
}
It is planned to remove this restriction in the future but currently creates
2016-06-03 09:42:48 +00:00
some complications because of how arrays are passed in the ABI.
2016-03-14 14:43:27 +00:00
2015-12-07 20:16:25 +00:00
.. index:: ! array;length, length, push, !array;push
Members
^^^^^^^
**length**:
Arrays have a ``length`` member to hold their number of elements.
2015-12-07 20:16:25 +00:00
Dynamic arrays can be resized in storage (not in memory) by changing the
``.length`` member. This does not happen automatically when attempting to access elements outside the current length. The size of memory arrays is fixed (but dynamic, i.e. it can depend on runtime parameters) once they are created.
2015-12-07 20:16:25 +00:00
**push**:
Dynamic storage arrays and ``bytes`` (not ``string``) have a member function called ``push`` that can be used to append an element at the end of the array. The function returns the new length.
2015-12-07 20:16:25 +00:00
.. warning::
It is not yet possible to use arrays of arrays in external functions.
.. warning::
Due to limitations of the EVM, it is not possible to return
dynamic content from external function calls. The function ``f`` in
``contract C { function f() returns (uint[]) { ... } }`` will return
2015-12-07 20:16:25 +00:00
something if called from web3.js, but not if called from Solidity.
The only workaround for now is to use large statically-sized arrays.
::
2017-12-12 18:47:30 +00:00
pragma solidity ^0.4.16;
2015-12-07 20:16:25 +00:00
contract ArrayContract {
2016-05-18 15:05:28 +00:00
uint[2**20] m_aLotOfIntegers;
2017-06-12 15:21:19 +00:00
// Note that the following is not a pair of dynamic arrays but a
// dynamic array of pairs (i.e. of fixed size arrays of length two).
2016-05-18 15:05:28 +00:00
bool[2][] m_pairsOfFlags;
// newPairs is stored in memory - the default for function arguments
2017-12-12 18:47:30 +00:00
function setAllFlagPairs(bool[2][] newPairs) public {
2016-05-18 15:05:28 +00:00
// assignment to a storage array replaces the complete array
m_pairsOfFlags = newPairs;
}
2017-12-12 18:47:30 +00:00
function setFlagPair(uint index, bool flagA, bool flagB) public {
2016-05-18 15:05:28 +00:00
// access to a non-existing index will throw an exception
m_pairsOfFlags[index][0] = flagA;
m_pairsOfFlags[index][1] = flagB;
}
2017-12-12 18:47:30 +00:00
function changeFlagArraySize(uint newSize) public {
2016-05-18 15:05:28 +00:00
// if the new size is smaller, removed array elements will be cleared
m_pairsOfFlags.length = newSize;
}
2017-12-12 18:47:30 +00:00
function clear() public {
2016-05-18 15:05:28 +00:00
// these clear the arrays completely
delete m_pairsOfFlags;
delete m_aLotOfIntegers;
// identical effect here
m_pairsOfFlags.length = 0;
}
bytes m_byteData;
2017-12-12 18:47:30 +00:00
function byteArrays(bytes data) public {
2016-05-18 15:05:28 +00:00
// byte arrays ("bytes") are different as they are stored without padding,
// but can be treated identical to "uint8[]"
m_byteData = data;
m_byteData.length += 7;
2017-12-12 18:47:30 +00:00
m_byteData[3] = byte(8);
2016-05-18 15:05:28 +00:00
delete m_byteData[2];
}
2017-12-12 18:47:30 +00:00
function addFlag(bool[2] flag) public returns (uint) {
2016-05-18 15:05:28 +00:00
return m_pairsOfFlags.push(flag);
}
2017-12-12 18:47:30 +00:00
function createMemoryArray(uint size) public pure returns (bytes) {
2016-05-18 15:05:28 +00:00
// Dynamic memory arrays are created using `new`:
uint[2][] memory arrayOfPairs = new uint[2][](size);
// Create a dynamic byte array:
bytes memory b = new bytes(200);
for (uint i = 0; i < b.length; i++)
2018-05-11 12:06:31 +00:00
b[i] = byte(uint8(i));
2016-05-18 15:05:28 +00:00
return b;
}
2015-12-07 20:16:25 +00:00
}
.. index:: ! struct, ! type;struct
.. _structs:
2015-12-07 20:16:25 +00:00
Structs
-------
Solidity provides a way to define new types in the form of structs, which is
shown in the following example:
::
2017-04-19 18:12:45 +00:00
pragma solidity ^0.4.11;
2015-12-07 20:16:25 +00:00
contract CrowdFunding {
2016-05-18 15:05:28 +00:00
// Defines a new type with two fields.
struct Funder {
address addr;
uint amount;
}
struct Campaign {
address beneficiary;
uint fundingGoal;
uint numFunders;
uint amount;
mapping (uint => Funder) funders;
}
uint numCampaigns;
mapping (uint => Campaign) campaigns;
2017-12-12 18:47:30 +00:00
function newCampaign(address beneficiary, uint goal) public returns (uint campaignID) {
2016-05-18 15:05:28 +00:00
campaignID = numCampaigns++; // campaignID is return variable
// Creates new struct and saves in storage. We leave out the mapping type.
campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
}
2017-12-12 18:47:30 +00:00
function contribute(uint campaignID) public payable {
2017-07-26 13:47:03 +00:00
Campaign storage c = campaigns[campaignID];
2015-12-07 20:16:25 +00:00
// Creates a new temporary memory struct, initialised with the given values
// and copies it over to storage.
// Note that you can also use Funder(msg.sender, msg.value) to initialise.
2016-05-18 15:05:28 +00:00
c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value});
c.amount += msg.value;
}
2017-12-12 18:47:30 +00:00
function checkGoalReached(uint campaignID) public returns (bool reached) {
2017-07-26 13:47:03 +00:00
Campaign storage c = campaigns[campaignID];
2016-05-18 15:05:28 +00:00
if (c.amount < c.fundingGoal)
return false;
2016-09-05 14:29:08 +00:00
uint amount = c.amount;
2016-05-18 15:05:28 +00:00
c.amount = 0;
2017-04-19 18:12:45 +00:00
c.beneficiary.transfer(amount);
2016-05-18 15:05:28 +00:00
return true;
}
2015-12-07 20:16:25 +00:00
}
The contract does not provide the full functionality of a crowdfunding
contract, but it contains the basic concepts necessary to understand structs.
Struct types can be used inside mappings and arrays and they can itself
contain mappings and arrays.
It is not possible for a struct to contain a member of its own type,
although the struct itself can be the value type of a mapping member.
This restriction is necessary, as the size of the struct has to be finite.
Note how in all the functions, a struct type is assigned to a local variable
(of the default storage data location).
This does not copy the struct but only stores a reference so that assignments to
members of the local variable actually write to the state.
Of course, you can also directly access the members of the struct without
assigning it to a local variable, as in
``campaigns[campaignID].amount = 0``.
2015-12-07 20:16:25 +00:00
.. index:: !mapping
Mappings
========
2016-12-21 22:27:31 +00:00
Mapping types are declared as ``mapping(_KeyType => _ValueType)``.
Here ``_KeyType`` can be almost any type except for a mapping, a dynamically sized array, a contract, an enum and a struct.
``_ValueType`` can actually be any type, including mappings.
2015-12-07 20:16:25 +00:00
2017-06-12 08:12:09 +00:00
Mappings can be seen as `hash tables <https://en.wikipedia.org/wiki/Hash_table>`_ which are virtually initialized such that
2015-12-07 20:16:25 +00:00
every possible key exists and is mapped to a value whose byte-representation is
all zeros: a type's :ref:`default value <default-value>`. The similarity ends here, though: The key data is not actually stored
in a mapping, only its ``keccak256`` hash used to look up the value.
2015-12-07 20:16:25 +00:00
Because of this, mappings do not have a length or a concept of a key or value being "set".
Mappings are only allowed for state variables (or as storage reference types
in internal functions).
2017-07-01 03:31:31 +00:00
It is possible to mark mappings ``public`` and have Solidity create a :ref:`getter <visibility-and-getters>`.
2017-02-02 23:52:34 +00:00
The ``_KeyType`` will become a required parameter for the getter and it will
2016-10-15 21:50:46 +00:00
return ``_ValueType``.
2017-02-02 23:52:34 +00:00
The ``_ValueType`` can be a mapping too. The getter will have one parameter
for each ``_KeyType``, recursively.
2016-10-15 21:50:46 +00:00
::
pragma solidity ^0.4.0;
contract MappingExample {
mapping(address => uint) public balances;
2017-12-12 18:47:30 +00:00
function update(uint newBalance) public {
2016-10-15 21:50:46 +00:00
balances[msg.sender] = newBalance;
}
}
contract MappingUser {
2017-12-12 18:47:30 +00:00
function f() public returns (uint) {
MappingExample m = new MappingExample();
m.update(100);
return m.balances(this);
2016-10-15 21:50:46 +00:00
}
}
.. note::
Mappings are not iterable, but it is possible to implement a data structure on top of them.
For an example, see `iterable mapping <https://github.com/ethereum/dapp-bin/blob/master/library/iterable_mapping.sol>`_.
2015-12-07 20:16:25 +00:00
.. index:: assignment, ! delete, lvalue
Operators Involving LValues
===========================
If ``a`` is an LValue (i.e. a variable or something that can be assigned to), the following operators are available as shorthands:
2015-12-07 20:16:25 +00:00
``a += e`` is equivalent to ``a = a + e``. The operators ``-=``, ``*=``, ``/=``, ``%=``, ``|=``, ``&=`` and ``^=`` are defined accordingly. ``a++`` and ``a--`` are equivalent to ``a += 1`` / ``a -= 1`` but the expression itself still has the previous value of ``a``. In contrast, ``--a`` and ``++a`` have the same effect on ``a`` but return the value after the change.
2015-12-07 20:16:25 +00:00
delete
------
``delete a`` assigns the initial value for the type to ``a``. I.e. for integers it is equivalent to ``a = 0``, but it can also be used on arrays, where it assigns a dynamic array of length zero or a static array of the same length with all elements reset. For structs, it assigns a struct with all members reset.
2015-12-07 20:16:25 +00:00
``delete`` has no effect on whole mappings (as the keys of mappings may be arbitrary and are generally unknown). So if you delete a struct, it will reset all members that are not mappings and also recurse into the members unless they are mappings. However, individual keys and what they map to can be deleted.
2015-12-07 20:16:25 +00:00
It is important to note that ``delete a`` really behaves like an assignment to ``a``, i.e. it stores a new object in ``a``.
2015-12-07 20:16:25 +00:00
::
2016-09-05 14:29:08 +00:00
pragma solidity ^0.4.0;
2015-12-07 20:16:25 +00:00
contract DeleteExample {
2016-05-18 15:05:28 +00:00
uint data;
uint[] dataArray;
2017-12-12 18:47:30 +00:00
function f() public {
2016-05-18 15:05:28 +00:00
uint x = data;
delete x; // sets x to 0, does not affect data
delete data; // sets data to 0, does not affect x which still holds a copy
2017-12-12 18:47:30 +00:00
uint[] storage y = dataArray;
2016-05-18 15:05:28 +00:00
delete dataArray; // this sets dataArray.length to zero, but as uint[] is a complex object, also
// y is affected which is an alias to the storage object
// On the other hand: "delete y" is not valid, as assignments to local variables
// referencing storage objects can only be made from existing storage objects.
}
2015-12-07 20:16:25 +00:00
}
.. index:: ! type;conversion, ! cast
Conversions between Elementary Types
====================================
Implicit Conversions
--------------------
If an operator is applied to different types, the compiler tries to
implicitly convert one of the operands to the type of the other (the same is
true for assignments). In general, an implicit conversion between value-types
is possible if it
makes sense semantically and no information is lost: ``uint8`` is convertible to
``uint16`` and ``int128`` to ``int256``, but ``int8`` is not convertible to ``uint256``
(because ``uint256`` cannot hold e.g. ``-1``).
2015-12-07 20:16:25 +00:00
Furthermore, unsigned integers can be converted to bytes of the same or larger
size, but not vice-versa. Any type that can be converted to ``uint160`` can also
be converted to ``address``.
2015-12-07 20:16:25 +00:00
Explicit Conversions
--------------------
If the compiler does not allow implicit conversion but you know what you are
2016-08-25 18:19:35 +00:00
doing, an explicit type conversion is sometimes possible. Note that this may
give you some unexpected behaviour so be sure to test to ensure that the
result is what you want! Take the following example where you are converting
a negative ``int8`` to a ``uint``:
::
2015-12-07 20:16:25 +00:00
int8 y = -3;
uint x = uint(y);
At the end of this code snippet, ``x`` will have the value ``0xfffff..fd`` (64 hex
2016-08-25 18:19:35 +00:00
characters), which is -3 in the two's complement representation of 256 bits.
2015-12-07 20:16:25 +00:00
If a type is explicitly converted to a smaller type, higher-order bits are
cut off::
uint32 a = 0x12345678;
uint16 b = uint16(a); // b will be 0x5678 now
Since 0.5.0 explicit conversions between integers and fixed-size byte arrays
are only allowed, if both have the same size. To convert between integers and
fixed-size byte arrays of different size, they first have to be explicitly
converted to a matching size. This makes alignment and padding explicit::
uint16 x = 0xffff;
bytes32(uint256(x)); // pad on the left
bytes32(bytes2(x)); // pad on the right
2015-12-07 20:16:25 +00:00
.. index:: ! type;deduction, ! var
.. _type-deduction:
Type Deduction
==============
For convenience, it is not always necessary to explicitly specify the type of a
variable, the compiler automatically infers it from the type of the first
expression that is assigned to the variable::
2016-05-30 17:36:14 +00:00
uint24 x = 0x123;
2015-12-07 20:16:25 +00:00
var y = x;
2016-05-30 17:36:14 +00:00
Here, the type of ``y`` will be ``uint24``. Using ``var`` is not possible for function
2015-12-07 20:16:25 +00:00
parameters or return parameters.
.. warning::
The type is only deduced from the first assignment, so
the loop in the following snippet is infinite, as ``i`` will have the type
``uint8`` and the highest value of this type is smaller than ``2000``.
``for (var i = 0; i < 2000; i++) { ... }``
2015-12-07 20:16:25 +00:00