In Truffle's pet-shop tutorial. Adoption.sol is --
pragma solidity ^0.4.19;
contract Adoption {
address[16] public adopters;
// Adopting a pet
function adopt(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = msg.sender;
return petId;
}
}
TestAdoption.sol is --
pragma solidity ^0.4.19;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Adoption.sol";
contract TestAdoption {
Adoption adoption = Adoption(DeployedAddresses.Adoption());
// Testing retrieval of a single pet's owner
function testGetAdopterAddressByPetId() public {
// Expected owner is this contract
address expected = this;
address adopter = adoption.adopters(8);
Assert.equal(adopter, expected, "Owner of pet ID 8.");
}
}
My question is on the line address adopter = adoption.adopters(8);
in TestAdoption.sol I thought adoption.adopters is an array and its element should be accessed via [8]
instead of (8)
.
However, Truffle's code is correct. In fact, changing (8)
to [8]
will result in Solidity compilation error --
TypeError: Indexed expression has to be a type, mapping or array (is function (uint256) view external returns (address))
address adopter = adoption.adopters[8];
^---------------^
What's the theory behind?