# Immutable variables cannot be initialized

**URL:** <https://forum.openzeppelin.com/t/immutable-variables-cannot-be-initialized/7927>\
**Category:** Upgrades\
**Created:** [May 7, 2021, 12:11pm UTC](https://forum.openzeppelin.com/t/immutable-variables-cannot-be-initialized/7927 "2021-05-07T12:11:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxareo](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/maxareo/32/11606_2.png) [@maxareo](https://forum.openzeppelin.com/u/maxareo)\
**Post date:** [May 7, 2021, 12:11pm UTC](https://forum.openzeppelin.com/t/immutable-variables-cannot-be-initialized/7927/1 "2021-05-07T12:11:37Z")

</div>

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.`

---

<div class="post-metadata">

**Author:** ![Skyge](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/skyge/32/343_2.png) [@Skyge](https://forum.openzeppelin.com/u/Skyge)\
**Post date:** [May 7, 2021, 1:12pm UTC](https://forum.openzeppelin.com/t/immutable-variables-cannot-be-initialized/7927/2 "2021-05-07T13:12:53Z")

</div>

Could you please share your code?

---

<div class="post-metadata">

**Author:** ![maxareo](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/maxareo/32/11606_2.png) [@maxareo](https://forum.openzeppelin.com/u/maxareo)\
**Post date:** [May 7, 2021, 1:59pm UTC](https://forum.openzeppelin.com/t/immutable-variables-cannot-be-initialized/7927/3 "2021-05-07T13:59:15Z")

</div>

```
// 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;
    }
}
```

---

<div class="post-metadata">

**Author:** ![martriay](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/martriay/32/20_2.png) [@martriay](https://forum.openzeppelin.com/u/martriay)\
**Post date:** [May 7, 2021, 2:24pm UTC](https://forum.openzeppelin.com/t/immutable-variables-cannot-be-initialized/7927/4 "2021-05-07T14:24:49Z")

</div>

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](https://docs.soliditylang.org/en/v0.8.4/contracts.html#immutable)):

> 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.

---

<div class="post-metadata">

**Author:** ![maxareo](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/maxareo/32/11606_2.png) [@maxareo](https://forum.openzeppelin.com/u/maxareo)\
**Post date:** [May 7, 2021, 3:07pm UTC](https://forum.openzeppelin.com/t/immutable-variables-cannot-be-initialized/7927/5 "2021-05-07T15:07:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martriay](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/martriay/32/20_2.png) [@martriay](https://forum.openzeppelin.com/u/martriay)\
**Post date:** [May 7, 2021, 3:39pm UTC](https://forum.openzeppelin.com/t/immutable-variables-cannot-be-initialized/7927/6 "2021-05-07T15:39:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maxareo](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/maxareo/32/11606_2.png) [@maxareo](https://forum.openzeppelin.com/u/maxareo)\
**Post date:** [May 7, 2021, 11:01pm UTC](https://forum.openzeppelin.com/t/immutable-variables-cannot-be-initialized/7927/7 "2021-05-07T23:01:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dtp5](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/dtp5/32/2589_2.png) [@dtp5](https://forum.openzeppelin.com/u/dtp5)\
**Post date:** [February 1, 2022, 6:54pm UTC](https://forum.openzeppelin.com/t/immutable-variables-cannot-be-initialized/7927/8 "2022-02-01T18:54:55Z")

</div>

Checking in to see if it's still not possible to achieve non-inlined `immutable` variables.

`immutable` variables would be significantly cheaper to reference than if they were loaded from storage. And some variables, like external proxy addresses, may never need to be updated, so it wouldn't make sense for these variables to _not_ be `immutable` or `constant`.

Using `constant` would require a pre-processor like solpp to replace `$(VARIABLE)` at compile time, which is much more complex across multiple contracts, deployments, and networks, and thus less safe.

Below, contracts `B` and `C` keep a reference to `A`, but only `C`'s reference is `immutable`. Why is it not possible to deploy `C` with a `constructor` that sets `a` before linking `C` to a proxy that can initialize it?

```auto
contract A {
    uint256 public x = 5;

    function getX() external view returns (uint256) {
        return x;
    }
}

contract B is Initializable {
    A public a;

    function initialize(A _a) public initializer {
        a = _a;
    }

    function getX() public view returns (uint256) {
        // This cold-loads `a` from storage.
        return a.getX();
    }
}

contract C is Initializable {
    A public immutable a;

    constructor(A _a) {
        a = _a;
    }

    function initialize() public initializer {}

    function getX() public view returns (uint256) {
        // This loads `a` from bytecode.
        return a.getX();
    }
}

```
