-
Notifications
You must be signed in to change notification settings - Fork 6.1k
DOCS: Move String literal and inline array FAQ items #5437
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
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #5437 +/- ##
===========================================
+ Coverage 88.12% 88.19% +0.07%
===========================================
Files 321 314 -7
Lines 31712 31419 -293
Branches 3809 3770 -39
===========================================
- Hits 27945 27710 -235
+ Misses 2481 2444 -37
+ Partials 1286 1265 -21
|
394e379
to
6eae939
Compare
Rebased and squashed |
docs/types.rst
Outdated
|
||
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``. | ||
|
||
For example, with ``"stringliteral"`` the string literal is interpreted in its raw byte form and with certain tools, if you inspect it, you see a 32-byte hex value, which is the ``"stringliteral"`` in hex. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the section is better without this paragraph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
docs/types.rst
Outdated
|
||
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``. | ||
|
||
For example, with ``"stringliteral"`` the string literal is interpreted in its raw byte form and with certain tools, if you inspect it, you see a 32-byte hex value, which is the ``"stringliteral"`` in hex. | ||
|
||
A string literal becomes a ``string`` type when you assign it to a variable, for example:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the comment above ("can be converted") the same as here? Also note that in its current form, it is incorrect, because it says string
here and bytes32
below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are slightly different in explanation, but now the above is deleted anyway (https://github.com/ethereum/solidity/pull/5437/files#r236217406) I think the duplication is gone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, first I realised that the FAQ point matching this hadn't been removed in this new PR, and rereading that I clarified this text to make sense. But it may be now that the addition isn't so valuable.
docs/types.rst
Outdated
@@ -902,6 +908,23 @@ possible: | |||
It is planned to remove this restriction in the future but currently creates | |||
some complications because of how arrays are passed in the ABI. | |||
|
|||
.. index:: !inline;arrays | |||
|
|||
Inline Arrays |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline arrays and array literals are two words for the same thing.
docs/types.rst
Outdated
Inline Arrays | ||
^^^^^^^^^^^^^ | ||
|
||
You can initialise a statically sized memory array inline using syntax such as ``string[] memory myarray = new string[](4);``. For example:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not complicate it my introducing the term "inline initializing" - it is just an assignment to a variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think these FAQ items provide any substantial value and would propose to just delete them.
Hmm @chriseth while the FAQ items maybe didn't add much, through this discussion we've ended clarifying a couple of different things. Have a look at the latest changes that attempt to combine and consolidate the points more, and maybe the PR has grown slightly in scope, but it started with trying to merge the content from the PR. |
docs/types.rst
Outdated
|
||
Array literals are arrays that are written as an expression and are not | ||
Array literals (or inline arrays) are arrays that are written as an expression and are not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps more explicit "also called"?
docs/types.rst
Outdated
assigned to a variable right away. | ||
|
||
You create a statically sized memory array literal using syntax such as | ||
``string[] memory myarray = new string[](4);``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no array literal in this example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example below is correct, only this one does not fit this section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This came from the FAQ point, I think the inline example is enough anyway, so let's just remove the longer example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example here is wrong. The one you removed is correct, and I think it is a good example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think something got lost in Git history, I think this is the example you mean, i.e. the one that's in current docs?
Do you think it's still worth adding something to show people how to initialise inline? I know it's quite simple syntax, but just for completeness. And that was one of the points made in the removed FAQ point.
docs/types.rst
Outdated
} | ||
} | ||
|
||
You create multi-dimensional arrays in the same way, but filling arrays can |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this paragraph.
docs/types.rst
Outdated
|
||
The type of an array literal is a memory array of fixed size whose base | ||
type is the common type of the given elements. For example, the type of ``[1, 2, 3]`` is | ||
``uint8[3] memory``, because the type of each of these constants is ``uint8``. Because of this, it is necessary to convert the first element to ``uint``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps replace one of the becauses by a different word.
docs/types.rst
Outdated
|
||
pragma solidity >=0.4.16 <0.6.0; | ||
|
||
contract C { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the correct example, but it's the same as the one some lines further down...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I wanted to have a syntax example, even if it's really basic, and somewhat unrealistic. Hopefully this is OK.
docs/types.rst
Outdated
The type of an array literal is a memory array of fixed size whose base | ||
type is the common type of the given elements. For example, the type of ``[1, 2, 3]`` is | ||
``uint8[3] memory``, because the type of each of these constants is ``uint8``, | ||
it is necessary to convert the first element to ``uint``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is ONLY necessary to convert it if you want the result to be a uint[3] memory
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, hopefully I understood you correctly.
docs/types.rst
Outdated
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
contract C { | ||
function f() public pure { | ||
uint[] memory a; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not an inline array. It is a declaration of a variable of array type. If you want to have a very simple example that is different from the one below, it might be better to just use a snippet and not a full contract. Something like [1, x, y]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chriseth OK, I must admit, I found the opening paragraph super confusing, and it was leading to my confusion and continual wrong examples. Unless I have completely miss-understood something (and I read some other language docs to clarify), I changed that opening paragraph (paraphrased from the JS MDN docs), and then the example.
Hopefully that clears up my confusion, and maybe the confusion of others.
docs/types.rst
Outdated
uint[] memory a; | ||
} | ||
} | ||
An array literal is a list of zero or more array elements, enclosed in square |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think zero elements are possible. At least the compiler would not be able to assign a type to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from that, it sounds really good :)
d89b48b
to
ba58719
Compare
docs/types.rst
Outdated
Array Literals / Inline Arrays | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
An array literal is a comma-separated list of one or more expressions, enclosed in square | ||
brackets (``[...]``). For example ``[1, a, f(3)]``. The elements must have a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all expressions always have a type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume #5437 (comment) replaces this?
docs/types.rst
Outdated
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
An array literal is a comma-separated list of one or more expressions, enclosed in square | ||
brackets (``[...]``). For example ``[1, a, f(3)]``. The elements must have a | ||
type so they can all be implicitly converted to and this is the elementary type of the array. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: There must be a common type all elements can be implicitly converted to. This is the elementary type of the array.
Array literals are arrays that are written as an expression and are not | ||
assigned to a variable right away. | ||
In the example below, the type of ``[1, 2, 3]`` is | ||
``uint8[3] memory``. Because the type of each of these constants is ``uint8``, if you want the result to be a ``uint[3] memory`` type, you need to convert the first element to ``uint``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you start a sentence with because
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can in certain cases, and this one is OK :)
Squashing. |
Fix tab Update docs/types.rst Co-Authored-By: ChrisChinchilla <chriswhward@gmail.com> Update docs/types.rst Co-Authored-By: ChrisChinchilla <chriswhward@gmail.com>
4d4f4b6
to
78ca280
Compare
Description
This PR moves String literal and inline array FAQ items, breaking apart https://github.com/ethereum/solidity/pull/4499/files
Checklist