Immutable variables cannot be initialized

Got an error when assigning a value to an immutable variable in the initialize() function. Wondering if there is a workaround for immutable variables when using an Initializable contract.

TypeError: Cannot write to immutable here: Immutable variables can only be initialized inline or assigned directly in the constructor.

Could you please share your code?

// SPDX-License-Identifier: MIT

pragma solidity 0.8.4;

import "@openzeppelin/contracts/proxy/utils/Initializable.sol";

contract Testing is Initializable {
    uint256 private immutable MAX_SUPPLY;

    constructor() {}

    function initialize(uint256 a, uint256 b) public initializer {
        MAX_SUPPLY = a * b;

As the error says, you cannot assign an immutable variable in any other place aside from the constructor or inlined in the contract body. Here’s why (from the Solidity documentation):

The contract creation code generated by the compiler will modify the contract’s runtime code before it is returned by replacing all references to immutables by the values assigned to the them.

At deployment/creation time, the contract creation code will replace all references to the immutable var by its value (which comes either with the contract code if it’s inlined or as an argument if it’s in the constructor), but has no way to know what a and b for initialize will be.

That implies the workaround is not to use an immutable variable in an Initializable contract, correct?

1 Like

Unless you’re ok with an inlined fixed value, that’s correct :slight_smile:

That would defeat the purpose of using an immutable variable. One could just use constant. I think just being careful and never modifying the variable should work just fine. Thanks man.