-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Enrich document with using getter function over state struct instance #15526
Open
haoyang9804
wants to merge
22
commits into
ethereum:develop
Choose a base branch
from
haoyang9804:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+59
−26
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6170061
enrich document with using getter function over state struct instance
haoyang9804 34c53d6
fix errors in the doc
haoyang9804 4015242
remove Trailing whitespace
haoyang9804 40b83b3
Merge branch 'develop' into develop
haoyang9804 dffaa5d
Refine doc based on suggestions
haoyang9804 0e9fc9f
refine the doc
haoyang9804 c23ad79
Merge branch 'develop' into develop
haoyang9804 79932bf
Merge branch 'develop' into develop
haoyang9804 a84241c
merge array example and struct example. Remove mapping.
haoyang9804 046cb36
Merge branch 'develop' into develop
haoyang9804 daedcd0
Document a special workaround for calldata arrays and struct instances
haoyang9804 f01f098
reset the incorrect commit
haoyang9804 7a2d0d9
Merge branch 'develop' into develop
haoyang9804 4218d58
Merge branch 'develop' into develop
haoyang9804 b8a072b
refine the doc
haoyang9804 17c365b
Merge branch 'develop' into develop
haoyang9804 f3b721f
Fix errors in the code example
haoyang9804 9052b55
Merge branch 'develop' of github.com:haoyang9804/solidity into develop
haoyang9804 615c5a8
Merge branch 'develop' into develop
haoyang9804 c814202
refine the doc
haoyang9804 901bef0
Merge branch 'develop' of github.com:haoyang9804/solidity into develop
haoyang9804 7a0e70d
Merge branch 'develop' into develop
haoyang9804 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,19 +157,19 @@ it evaluates to a state variable. If it is accessed externally | |
} | ||
} | ||
|
||
When considering arrays, mapping, and structs, the getter functions do not return | ||
the entire state variable. | ||
|
||
If you have a ``public`` state variable of array type, then you can only retrieve | ||
single elements of the array via the generated getter function. This mechanism | ||
exists to avoid high gas costs when returning an entire array. You can use | ||
arguments to specify which individual element to return, for example | ||
``myArray(0)``. If you want to return an entire array in one call, then you need | ||
to write a function, for example: | ||
exists to avoid high gas costs when returning an entire array. | ||
|
||
.. code-block:: solidity | ||
|
||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity >=0.4.16 <0.9.0; | ||
|
||
contract arrayExample { | ||
contract ArrayExample { | ||
// public state variable | ||
uint[] public myArray; | ||
|
||
|
@@ -186,39 +186,72 @@ to write a function, for example: | |
} | ||
} | ||
|
||
Now you can use ``getArray()`` to retrieve the entire array, instead of | ||
``myArray(i)``, which returns a single element per call. | ||
If you have a ``public`` state variable of mapping type. The getter function | ||
returns the value associated with the key passed as an argument. | ||
|
||
.. code-block:: solidity | ||
|
||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity >=0.4.16 <0.9.0; | ||
|
||
contract MappingExample { | ||
// public state variable | ||
mapping(uint => uint) public myMap; | ||
|
||
// Getter function generated by the compiler | ||
/* | ||
function myMap(uint i) public view returns (uint) { | ||
return myMap[i]; | ||
} | ||
*/ | ||
} | ||
|
||
The next example is more complex: | ||
When you declare a public state variable of a struct type, the generated getter function | ||
returns specific member of the struct as separate elements within a tuple, | ||
rather than the struct as a single object in memory. | ||
The members returned appear in the order they are declared in the struct, provided they | ||
are not omitted. Array members and mapping members within the struct are excluded from the returned tuple. | ||
Additionally, if the struct consists solely of array members and mapping members, no getter function will be generated. | ||
|
||
.. code-block:: solidity | ||
|
||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity >=0.4.0 <0.9.0; | ||
pragma solidity >=0.4.16 <0.9.0; | ||
|
||
contract Complex { | ||
struct Data { | ||
contract StructExample { | ||
// Struct Definition with single member | ||
struct SimpleStruct { | ||
int[2] arr; | ||
mapping(uint => uint) map; | ||
} | ||
|
||
// Struct Definition with multiple members | ||
struct ComplexStruct { | ||
uint a; | ||
bytes3 b; | ||
mapping(uint => uint) map; | ||
uint[3] c; | ||
uint[] d; | ||
bytes e; | ||
} | ||
mapping(uint => mapping(bool => Data[])) public data; | ||
} | ||
|
||
It generates a function of the following form. The mapping and arrays (with the | ||
exception of byte arrays) in the struct are omitted because there is no good way | ||
to select individual struct members or provide a key for the mapping: | ||
|
||
.. code-block:: solidity | ||
// public state variables | ||
// SimpleStruct omits all its members, therefore its public instance is disallowed | ||
ComplexStruct public complexStruct; | ||
mapping(uint => mapping(bool => ComplexStruct[])) public map; | ||
|
||
function data(uint arg1, bool arg2, uint arg3) | ||
public | ||
returns (uint a, bytes3 b, bytes memory e) | ||
{ | ||
a = data[arg1][arg2][arg3].a; | ||
b = data[arg1][arg2][arg3].b; | ||
e = data[arg1][arg2][arg3].e; | ||
} | ||
// Getter function generated by the compiler | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be nice to keep the explanation from before (with the correction i suggested in #15526 (comment)):
|
||
/* | ||
function complexStruct() public view returns (uint, bytes3, bytes memory) { | ||
return (complexStruct.a, complexStruct.b, complexStruct.e); | ||
} | ||
function map(uint arg1, bool arg2, uint arg3) | ||
public | ||
returns (uint a, bytes3 b, bytes memory e) | ||
{ | ||
a = map[arg1][arg2][arg3].a; | ||
b = map[arg1][arg2][arg3].b; | ||
e = map[arg1][arg2][arg3].e; | ||
} | ||
*/ | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.