My understanding is that immutable
variables are basically equivalent to constant
variables however the former is initialised in the constructor
whereas the later at declaration, however both are interpolated into the bytecode therefore no additional gas is incurred whenever they are used since they are not stored in state and hence do not have to be read from state.
Why then can functions that access immutable variables not be considered pure
whereas functions that access constant
variables are?
pragma solidity ^0.8.0;
contract Immutable {
uint constant number1 = 1;
uint immutable number2;
constructor() {
number2 = 2;
}
// compiles fine
function getNumber1() pure external returns(uint) {
return number1;
}
// compile error
function getNumber2() pure external returns(uint) {
return number2;
}
}
Best Answer
Not reading the storage is not the only requisite to be a pure function.
Summarizing discussions made on GitHub about this topic,
pure
implies that the output can be computed without any state, other than the functions themselves.From the official Solidity documentation:
Quoting chriseth: