2

According to this question the Solidity 0.5.0 Compiler cannot compute a constant state variable that invokes a function, however I'm also experiencing this issue on Solidity 0.8.10 using the ABDKMath64x64 library:

pragma solidity ^0.8.10;

import { ABDKMath64x64 } from "./ABDKMath64x64.sol";

contract FixedPoint {
    // TypeError Initial value for constant variable has to be compile-time constant
    int128 constant someFixedPointInteger = ABDKMath64x64.fromUInt(123);
}

So I instead opted to use immutable by initialising the state variable in the constructor as follows:

contract FixedPoint {
    int128 immutable someFixedPointInteger;

    constructor() {
        someFixedPointInteger = ABDKMath64x64.fromUInt(123);
    }
}

According to this answer:

constants aren't stored in storage anywhere; they're substituted in the bytecode

This obviously entails gas savings since no SLOAD is executed to retrieve constants from storage, since they're interpolated directly into the bytecode. Can the same be said about immutable variables that will result in similar gas savings?

1 Answer 1

2

Yes this is the case. This has also been outlined here What is the immutable keyword in Solidity? and can be found in the official Solidity docs here: https://docs.soliditylang.org/en/v0.8.11/contracts.html#constant-and-immutable-state-variables

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.