LEARN SOLIDITY LESSON
Storage & Memory in Solidity. Internal & External in Solidity
Storage & Memory in Solidity
Storage refers to variables stored permanently on the blockchain.
Memory variables are temporary, and are erased between external function calls to your contract.
Think of it like your computer's hard disk vs RAM.
Sandwich[] sandwiches; function eatSandwich(uint _index) public { // So instead, you should declare with the `storage` keyword Sandwich storage mySandwich = sandwiches[_index]; mySandwich.status = "Eaten!";
// If you just want a copy, you can use `memory`: Sandwich memory anotherSandwich = sandwiches[_index + 1]; anotherSandwich.status = "Eaten!"; sandwiches[_index + 1] = anotherSandwich; }
Internal & External in Solidity
Internal is the same as private, except that it's also accessible to contracts that inherit from this contract.
External is similar to public, except that these functions can ONLY be called outside the contract — they can't be called by other functions inside that contract.
contract Sandwich {
uint private sandwichesEaten = 0;
function eat() internal {
sandwichesEaten++;
}
}
contract BLT is Sandwich {
uint private baconSandwichesEaten = 0;
function eatWithBacon() public returns (string memory) {
baconSandwichesEaten++;
// We can call this here because it's internal
eat();
}
}
Comments