Skip to content

Commit

Permalink
update ctc
Browse files Browse the repository at this point in the history
  • Loading branch information
xhliu committed Aug 27, 2021
1 parent 824cd96 commit 5e7cc48
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
14 changes: 8 additions & 6 deletions docs/ctc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ Compile Time Constant
A compile time constant (CTC) is a value that can be computed at compile time. There are four types of compile time constants.

* literals
* static constants which are declared as contract properties and initialized with a literal
* static constants which are declared as function parameters and initialized with another CTC in a function call
* :ref:`induction variables<induction-var-label>`
* static constant properties of a contract, initialized with a literal
* static constant function parameters

There are four cases where only compile time constants are allowed.
There are several cases where only compile time constants are allowed.

* loop bound
* array size
* ``size`` in ``reverseBytes(bytes b, static const int size)``
* ``size`` in ``repeat(T e, static const int size)``
* write to an array element using :ref:`index operator<array-index-label>`
* function parameters that are declared as ``static const`` [#]_ such as ``size`` in ``reverseBytes(bytes b, static const int size)`` and ``repeat(T e, static const int size)``

.. code-block:: solidity
Expand Down Expand Up @@ -52,4 +52,6 @@ There are four cases where only compile time constants are allowed.
}
require(y == 1);
}
}
}
.. [#] Note: the order is important: ``const static`` is not allowed when declaring a function parameter, but works for a property.
7 changes: 1 addition & 6 deletions docs/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ Signature Verification
* ``bool checkSig(Sig sig, PubKey pk)``
* ``bool checkMultiSig(Sig[] sigs, PubKey[] pks)``

repeat()
--------
* ``T[size] repeat(T e, int size)``
returns an array with all ``size`` elements set to ``e``, where T can be any type.

``bytes`` Operations
--------------------
* Convert to and from ``int``
Expand Down Expand Up @@ -177,7 +172,7 @@ where the most significant bit indicates the sign (``0`` for positive, ``1`` for
bytes b = b'00112233' + b'334455' // b == b'00112233334455'
* ``reverseBytes(bytes b, int size)``
* ``reverseBytes(bytes b, static const int size)``

Returns reversed bytes of ``b``, which is of ``size`` bytes. Note ``size`` must be a :ref:`compile time constant<ctc-label>`.
It is often useful when converting a number between little-endian and big-endian.
Expand Down
8 changes: 6 additions & 2 deletions docs/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ An array is a fixed-size list of values of the same basic type.
int[] e = [1, 4, 2]; // e is of type int[3]
int[][] f = [[11, 12, 13], [21, 22, 23]]; // f is of type int[2][3]
* **Initialize/set an array to the same value** - Function ``T[size] repeat(T e, int size)`` returns an array with all ``size`` elements set to ``e``, where T can be any type.
* **Initialize/set an array to the same value** - Function ``T[size] repeat(T e, static const int size)`` returns an array with all ``size`` elements set to ``e``, where T can be any type.
Note ``size`` must be a :ref:`compile time constant<ctc-label>`.

.. code-block:: solidity
Expand All @@ -92,7 +92,8 @@ An array is a fixed-size list of values of the same basic type.
// set all flags to be false
flags = repeat(false, 4);
.. _array-index-label:

* **Index Operator** - index starting from 0. Out of bound access fails contract execution immediately.

.. code-block:: solidity
Expand All @@ -107,6 +108,9 @@ An array is a fixed-size list of values of the same basic type.
d = arr2D[idx][1];
// variable index is disallowed when writing into an array
a[idx] = 2;
// only a compile-time constant (CTC) can be used as an index when writing
a[2] = 2;
a[N] = 3; // N is a CTC
// assign to an array variable
a = arr2D[1];
// b is a new copy and the same as a
Expand Down

0 comments on commit 5e7cc48

Please sign in to comment.