typestar

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

  1. constant values live in the bytecode itself.
  2. immutable fields set once, in the constructor.
  3. msg.sender inside the constructor is the deployer.

Keywords and builtins used here

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.

Type this snippet

Step 4 of 4 in Contracts, step 4 of 28 in Language basics.

← Previous Next →