1

I am using REMIX IDE and on running the below code

// SPDX-License-Identifier:MIT

pragma solidity 0.8.6;


contract enum1{
    enum FreshJuice{small,medium,large}
    FreshJuice choice;
    FreshJuice constant defaultChoice=FreshJuice.medium;

    function setLarge() public{
        choice=FreshJuice.large;
    }

    function setSmall() public{
        choice=FreshJuice.small;
    }

    function getChoice() public view returns(FreshJuice){
        return choice;
    }

    function getDefault() public view returns(uint){
        return uint(defaultChoice);
    }
}

I am getting this warning

Warning: Function state mutability can be restricted to pure
  --> contracts/enumerator.sol:23:5:
   |
23 |     function getDefault() public view returns(uint){
   |     ^ (Relevant source part starts here and spans across multiple lines).

But after changing view to pure in line 23, it doesn't give warning.

// SPDX-License-Identifier:MIT

pragma solidity 0.8.6;


contract enum1{
    enum FreshJuice{small,medium,large}
    FreshJuice choice;
    FreshJuice constant defaultChoice=FreshJuice.medium;

    function setLarge() public{
        choice=FreshJuice.large;
    }

    function setSmall() public{
        choice=FreshJuice.small;
    }

    function getChoice() public view returns(FreshJuice){
        return choice;
    }

    function getDefault() public pure returns(uint){
        return uint(defaultChoice);
    }
}

Also how a pure function can read state variable.

1 Answer 1

1

Constants arent stored in the contracts storage, they actually are inlined in the code, so you are indeed not reading from storage. See https://docs.soliditylang.org/en/v0.8.21/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.