Skip to content

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

Merged
merged 1 commit into from
Dec 3, 2018

Conversation

ChrisChinchilla
Copy link
Contributor

@ChrisChinchilla ChrisChinchilla commented Nov 15, 2018

Description

This PR moves String literal and inline array FAQ items, breaking apart https://github.com/ethereum/solidity/pull/4499/files

Checklist

  • Code compiles correctly
  • All tests are passing
  • New tests have been created which fail without the change (if possible)
  • README / documentation was extended, if necessary
  • Changelog entry (if change is visible to the user)
  • Used meaningful commit messages

@codecov
Copy link

codecov bot commented Nov 23, 2018

Codecov Report

Merging #5437 into develop will increase coverage by 0.07%.
The diff coverage is n/a.

Impacted file tree graph

@@             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
Flag Coverage Δ
#all 88.19% <ø> (+0.07%) ⬆️
#syntax 29.05% <ø> (+0.08%) ⬆️

@ChrisChinchilla ChrisChinchilla force-pushed the docs-faq-types-inline-array branch from 394e379 to 6eae939 Compare November 26, 2018 09:50
@ChrisChinchilla
Copy link
Contributor Author

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.
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 the section is better without this paragraph.

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

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.

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is still incorrect.

Copy link
Contributor Author

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

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

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.

Copy link
Contributor

@chriseth chriseth left a 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.

@ChrisChinchilla
Copy link
Contributor Author

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

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

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.

Copy link
Contributor

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.

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 came from the FAQ point, I think the inline example is enough anyway, so let's just remove the longer example.

Copy link
Contributor

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.

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

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

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

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...

Copy link
Contributor Author

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

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.

Copy link
Contributor Author

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

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].

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

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.

Copy link
Contributor

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 :)

@chriseth chriseth force-pushed the docs-faq-types-inline-array branch from d89b48b to ba58719 Compare December 3, 2018 09:51
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
Copy link
Contributor

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.

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

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

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?

Copy link
Contributor Author

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 :)

@chriseth
Copy link
Contributor

chriseth commented Dec 3, 2018

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>
@chriseth chriseth force-pushed the docs-faq-types-inline-array branch from 4d4f4b6 to 78ca280 Compare December 3, 2018 10:49
@chriseth chriseth merged commit 04d9466 into develop Dec 3, 2018
@axic axic deleted the docs-faq-types-inline-array branch January 22, 2019 02:55
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.

4 participants