constant and immutable in Solidity
Baked at compile time, or fixed once at deployment.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// constant is baked at compile time; immutable at deployment.
contract Charter {
uint256 public constant MAX_PLOTS = 40;
address public immutable founder;
uint256 public immutable plantedYear;
constructor(uint256 year) {
founder = msg.sender;
plantedYear = year;
}
function age(uint256 now_) external view returns (uint256) {
return now_ - plantedYear;
}
}
How it works
constantvalues live in the bytecode itself.immutablefields set once, in the constructor.msg.senderinside the constructor is the deployer.
Keywords and builtins used here
addressconstructorcontractexternalfunctionpublicreturnreturnsuint256
The run, in numbers
- Lines
- 18
- Characters to type
- 417
- Tokens
- 61
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 61 seconds.
Step 4 of 4 in Contracts, step 4 of 28 in Language basics.