[Ethereum] Type error: memory is not implicitly convertible to expected type

contract-developmentmemorysoliditystorage

I have a function that returns a list of BetProposition smart contracts:

function getBetsForMatchup(uint matchupIdentifier) public returns (BetProposition[]) {
        return bets[matchupIdentifier];
    }

Later I initialize a local variable array as such, so I can loop through them:

   BetProposition[] betsToCancel = getBetsForMatchup(matchupIdentifier);

However, this doesn't seem to work and I get this error:

TypeError: Type contract BetProposition[] memory is not implicitly convertible to expected type contract BetProposition[] storage pointer.
    BetProposition[] betsToCancel = getBetsForMatchup(matchupIdentifier);
    ^------------------------------------------------------------------^

I'm not totally sure what this means. Any help appreciated. Thanks

Best Answer

The compiler thinks you are trying to store the return value of getBetsForMatchup in contract (permanent) storage.

Try changing

   BetProposition[] betsToCancel = getBetsForMatchup(matchupIdentifier);

to

   BetProposition[] memory betsToCancel = getBetsForMatchup(matchupIdentifier);

This compiled on remix:

   pragma solidity ^0.4.20;
   contract BetProposition {
       uint public somevar;

       function BetProposition(uint _some) public {
         somevar = _some;
       }
   }

   contract Tester2 {
        mapping(uint => BetProposition[]) bets;

        function getBetsForMatchup(uint matchupIdentifier) public view returns (BetProposition[]) {
            return bets[matchupIdentifier];
        }

        function other(uint matchupIdentifier) public view returns(uint) {
             uint total = 0;
             BetProposition[] memory betsToCancel = getBetsForMatchup(matchupIdentifier);
             for (uint i = 0; i < betsToCancel.length; i++) {
                 total = total + betsToCancel[i].somevar();
             }
             return total;
        }
}
Related Topic