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:
constant
s aren't stored in storage anywhere; they're substituted in the bytecode
This obviously entails gas savings since no SLOAD
is executed to retrieve constant
s 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?