【Solidity】Constant和immutable的区别
使用constant declare的变数都是无法更改的。但是有些常量只能够在合约运行的时候才能定义,无法预先写入代码内,这个时候就需要使用immutable。比如写 block.timestamp 或 msg.sender
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract test {
uint public constant x = 10;
address public immutable ownerAddress;
constructor(){
ownerAddress = msg.sender;
}
}
![]()
Facebook评论