Skip to content

Move type related FAQ points #4499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Conversation

ChrisChinchilla
Copy link
Contributor

@ChrisChinchilla ChrisChinchilla commented Jul 12, 2018

Partially closes #1219 including some formatting and language fixes in related points, additional questions below.

Also closes #2178.

docs/types.rst Outdated
@@ -609,6 +646,21 @@ number of bytes, always use one of ``bytes1`` to ``bytes32`` because they are mu
that you are accessing the low-level bytes of the UTF-8 representation,
and not the individual characters!

You can initialise a statically sized memory array inline using syntax such as ``string[] memory myarray = new string[](4);``. For example.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite complete, I'd like to match the text and the example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps better to move this into "Array literals / inline arrays"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

docs/types.rst Outdated

.. _type-deduction:

Type Deduction
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where this came from as I didn't add it, possibly from develop branch.

docs/types.rst Outdated
operators. For a quick reference of the various operators, see :ref:`order`.
Types can interact in expressions containing operators. For a quick reference of the various operators, see :ref:`order`.

Types can not return an ``undefined`` or "null" value. To handle any unexpected values you can 'throw' on error, which also reverts the whole transaction.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better: The concept of "undefined" or "null" does not exist in Solidity.

Also: Don't mention throw, rather use revert("Error message").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

docs/types.rst Outdated
@@ -52,6 +51,12 @@ Operators:
* Bit operators: ``&``, ``|``, ``^`` (bitwise exclusive or), ``~`` (bitwise negation)
* Arithmetic operators: ``+``, ``-``, unary ``-``, unary ``+``, ``*``, ``/``, ``%`` (remainder), ``**`` (exponentiation), ``<<`` (left shift), ``>>`` (right shift)

The bit size of an integer determines its range, for example, for ``uint256``, this is 0 up to 2 :sup:`256` -1. If the result of an operation doesn't fit inside this range, it is truncated. These truncations can have `serious consequences <https://en.bitcoin.it/wiki/Value_overflow_incident>`_, so use code like the below to avoid certain attacks and check a value is what you expect it to be.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The truncations themselves do not have serious consequences, rather not taking them into account might.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


::

require((balanceOf[_to] + _value) >= balanceOf[_to]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works for checking overflow for unsigned types, so we should make that clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

docs/types.rst Outdated

.. index:: literal, bytes
The string literal is interpreted in its raw byte form and if you inspect ``somevar``, you see a 32-byte hex value, which is the ``"stringliteral"`` in hex.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether or not it is hex depends on the tool you use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarified, might be nice to add a list at some point, will save that for a separate issue.

docs/types.rst Outdated
.. index:: literal, bytes
The string literal is interpreted in its raw byte form and if you inspect ``somevar``, you see a 32-byte hex value, which is the ``"stringliteral"`` in hex.

Using a ``string`` type is basically identical to ``bytes``, but it is assumed to hold the UTF-8 encoding of a real string. Since ``string`` UTF-8 encoding of some characters takes more than a single byte, it is quite expensive to compute the number of characters in the string. Because of this, Solidity does not support length methods ``string s; s.length``, or accessing an item at an array index ``s[2]``. If you want to access the low-level byte encoding of the string, you can use ``bytes(s).length`` and ``bytes(s)[2]`` which returns the number of bytes in the UTF-8 encoding of the string (not the number of characters) and the second byte (not character) of the UTF-8 encoded string, respectively.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only "using" but also the type itself is basically identical to bytes.

Note that strings is not a string literal, it is a string type, so we should probably move this somewhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attempted to split and consolidate, more changes might be required, again, probably in a new PR.

docs/types.rst Outdated

Using a ``string`` type is basically identical to ``bytes``, but it is assumed to hold the UTF-8 encoding of a real string. Since ``string`` UTF-8 encoding of some characters takes more than a single byte, it is quite expensive to compute the number of characters in the string. Because of this, Solidity does not support length methods ``string s; s.length``, or accessing an item at an array index ``s[2]``. If you want to access the low-level byte encoding of the string, you can use ``bytes(s).length`` and ``bytes(s)[2]`` which returns the number of bytes in the UTF-8 encoding of the string (not the number of characters) and the second byte (not character) of the UTF-8 encoded string, respectively.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pleas clarify whether \u takes hex or decimal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think done. I couldn't find any definitive Solidity doc mentioning it, so extrapolated from C and JavaScript docs.

docs/types.rst Outdated

.. index:: copy an array, copy a struct

Copying Reference Types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should also go into the newly created "copying, references, function calls, whatever" section with a link from here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriseth I think we should make this a new PR, so I'll revert this addition, keep the removal from the FAQ document and I'll stash this text for a forthcoming new PR.


::

require((balanceOf[_to] + _value) >= balanceOf[_to]);
Copy link
Member

@axic axic Sep 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bad idea without explanation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@axic I thought the text above and the example were enough, but added a little more, hopefully I got it right.

@chriseth chriseth closed this Nov 12, 2018
@ChrisChinchilla ChrisChinchilla deleted the faq-reorg-types branch November 14, 2018 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

doc: explain that state variables can be initialized inline Documentation: merge the FAQ in the appropriate sections
3 participants