-
Notifications
You must be signed in to change notification settings - Fork 7
/
SimpleStorage.sol
55 lines (40 loc) · 1.84 KB
/
SimpleStorage.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7; // ^ indicates 0.8.7 and above is ok for this contract
contract SimpleStorage {
// @notice This gets iniialized to zero by default.
// @notice If the scope of state variable is not defined, the default scope is "internal".
uint256 favoriteNumber;
mapping ( string => uint256 ) public nameToFavoriteNumber;
struct People {
uint256 favoriteNumber;
string name;
}
People[] public people;
// @notice One way to create an instance of type People
People public person1 = People(5, "Amit");
// @notice One more way to create an instance of type People
People public person2 = People({favoriteNumber: 12, name: "Jignesh"});
// @notice way to create multiple instance of people
// @notice 3 main storage locations - calldata, memory, storage
// @notice calldata are temporary variables that can't be modified
// @notice memory are teamprary variables that can be modified
// @notice storage is permanent
// @notice Data location can only be specified for array, struct or mapping types
function addPerson(string memory _name, uint256 _favoriteNumber) public {
// people.push(People(_favoriteNumber, _name));
People memory newPerson = People({favoriteNumber: _favoriteNumber, name: _name});
people.push(newPerson);
nameToFavoriteNumber[_name] = _favoriteNumber;
}
/** @notice Example using calldata
function addPerson(string calldata _name, uint256 _favoriteNumber) public {
_name = "any"; // Not possible and hence error as calldata variable cannot be modified.
}
*/
function store(uint256 _favoriteNumber) public {
favoriteNumber = _favoriteNumber;
}
function retrieve() public view returns(uint256) {
return favoriteNumber;
}
}