Skip to content
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
wants to merge 22 commits into
base: develop
Choose a base branch
from
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 Oct 18, 2024
34c53d6
fix errors in the doc
haoyang9804 Oct 18, 2024
4015242
remove Trailing whitespace
haoyang9804 Oct 18, 2024
40b83b3
Merge branch 'develop' into develop
haoyang9804 Oct 21, 2024
dffaa5d
Refine doc based on suggestions
haoyang9804 Oct 21, 2024
0e9fc9f
refine the doc
haoyang9804 Oct 21, 2024
c23ad79
Merge branch 'develop' into develop
haoyang9804 Oct 21, 2024
79932bf
Merge branch 'develop' into develop
haoyang9804 Oct 21, 2024
a84241c
merge array example and struct example. Remove mapping.
haoyang9804 Oct 21, 2024
046cb36
Merge branch 'develop' into develop
haoyang9804 Oct 22, 2024
daedcd0
Document a special workaround for calldata arrays and struct instances
haoyang9804 Oct 22, 2024
f01f098
reset the incorrect commit
haoyang9804 Oct 22, 2024
7a2d0d9
Merge branch 'develop' into develop
haoyang9804 Oct 23, 2024
4218d58
Merge branch 'develop' into develop
haoyang9804 Oct 24, 2024
b8a072b
refine the doc
haoyang9804 Oct 25, 2024
17c365b
Merge branch 'develop' into develop
haoyang9804 Oct 25, 2024
f3b721f
Fix errors in the code example
haoyang9804 Oct 26, 2024
9052b55
Merge branch 'develop' of github.com:haoyang9804/solidity into develop
haoyang9804 Oct 26, 2024
615c5a8
Merge branch 'develop' into develop
haoyang9804 Oct 30, 2024
c814202
refine the doc
haoyang9804 Oct 30, 2024
901bef0
Merge branch 'develop' of github.com:haoyang9804/solidity into develop
haoyang9804 Oct 30, 2024
7a0e70d
Merge branch 'develop' into develop
haoyang9804 Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 59 additions & 26 deletions docs/contracts/visibility-and-getters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
returns specific member of the struct as separate elements within a tuple,
returns each 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
Copy link
Collaborator

Choose a reason for hiding this comment

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

The mapping and arrays (with the exception of byte arrays)
in the struct are omitted because there is no good way to
select individual array members or provide a key for the mapping

/*
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;
}
*/
}