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 12 commits into
base: develop
Choose a base branch
from
87 changes: 61 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,74 @@ 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 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. 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
Additionally, if all struct members are omitted, 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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

have changed based on my understanding. plz check

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