forked from AmazingAng/WTF-Solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstant.sol
27 lines (24 loc) · 985 Bytes
/
Constant.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract Constant {
// constant变量必须在声明的时候初始化,之后不能改变
uint256 public constant CONSTANT_NUM = 10;
string public constant CONSTANT_STRING = "0xAA";
bytes public constant CONSTANT_BYTES = "WTF";
address public constant CONSTANT_ADDRESS = 0x0000000000000000000000000000000000000000;
// immutable变量可以在constructor里初始化,之后不能改变
uint256 public immutable IMMUTABLE_NUM = 9999999999;
address public immutable IMMUTABLE_ADDRESS;
uint256 public immutable IMMUTABLE_BLOCK;
uint256 public immutable IMMUTABLE_TEST;
// 利用constructor初始化immutable变量,因此可以利用
constructor(){
IMMUTABLE_ADDRESS = address(this);
IMMUTABLE_BLOCK = block.number;
IMMUTABLE_TEST = test();
}
function test() public pure returns(uint256){
uint256 what = 9;
return(what);
}
}