0

I wanted to create a getter function to access a mapping nested inside a struct. But the function that I made required me to use the expiremental version (I'm on Remix).

So I used the expiremental version to verify if the code I had done was correct and it was. Here the used code that I simplified as much as I could:

pragma solidity  ^0.5.16;
pragma experimental ABIEncoderV2;
contract SupChain{

enum StateType {Created, InTransit, Stored, OutOfComplicance, Completed}

struct Status{
    StateType state;
    address currentCounterparty;
    string test;
}

struct Order{
    uint256 orderID;
    uint256 NumberOfUpdate;
    string test;
    mapping (uint256 => Status) statutes;
}

mapping (uint256 => Order) public orders;

function getStatus(uint256 id, uint256 concernedStatusNumber)           
                   public returns (Status memory status){
    Order storage concernedOrder = orders[id];
    Status memory concernedStatus = concernedOrder.statutes[concernedStatusNumber];
    return concernedStatus;
}

Now I would like to be capable to access to the map without using the expiremental version. How do I code such a getter version ?

2

From last Solidity Documentation: https://solidity.readthedocs.io/en/v0.7.0/contracts.html#return-variables

At the end of RETURN-VARIABLES section in the NOTE paragraph: "You cannot return some types from non-internal functions, notably multi-dimensional dynamic arrays and structs. If you enable the new ABIEncoderV2 feature by adding pragma experimental ABIEncoderV2; to your source file then more types are available, but mapping types are still limited to inside a single contract and you cannot transfer them."

So you can return structs but only for internal calls. See also here: solidity-function-to-return-a-data-struct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.