Okay, I think the doc gives a bad example, yeah, I know, in order to use proxy pattern, I should set all initial values in an initializer function, so I think we can write like this:
Solidity allows defining initial values for fields when declaring them in a contract.
contract MyContract {
uint256 public hasInitialValue = 42; <<<<------- remove constant at here
}
This is equivalent to setting these values in the constructor, and as such, will not work for upgradeable contracts. Make sure that all initial values are set in an initializer function as shown below; otherwise, any upgradeable instances will not have these fields set.
contract MyContract is Initializable {
uint256 public hasInitialValue;
function initialize() public initializer {
hasInitialValue = 42;
}
}
So now we can say Note that it is still fine to define constant state variables in this way
,
contract MyContract {
uint256 public constant hasInitialValue = 42;
}
At least for me, this is more clear.