0

Note - Following is a contract that successfully compiled.

In this contract's constructor, I have clearly assigned multiple times to x. Why is this okay? What's happening here?


// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

contract IsThisImmutable {

    uint public s_a = 0;

    uint256 public immutable x;

    constructor() {
        x = 134;
        x += 3;
        s_a += x;
        x += 3;
    }

    function exp1(uint256 a) public returns(uint256) {
        s_a += a;
        s_a += x;
        return 34;
    }

    modifier modifier1 {
        _;
        s_a += 1023;
        return;
    }


}


1 Answer 1

1

It’s okay because you’re assigning value to x multiple times within the constructor itself, so the state change is only considered one time i.e., the final value that’s being assigned to it in the constructor.

4
  • But I can see that already... Commented Sep 17 at 7:52
  • Why is it considered only one time ? we have multiple sstore calls right ? Commented Sep 17 at 7:53
  • 1
    No SSTORE call is made, as immutable variables are stored in the contract bytecode, not in storage. Commented Sep 17 at 8:56
  • 1
    I'd also assume compiler optimizes this to only one assignment. Commented Sep 17 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.