0

Say I have a parent and a child contract, in the child I don't want to use one of the parent's functions, so I changed the logic to just revert if it is ever called.

Now the terminal output shows me this message after compile:

Warning: Function state mutability can be restricted to pure

Can I change the function mutability to pure without causing an error?

1 Answer 1

0

Yes, you may change the function's mutability to a more strict one.

See this quote from the solidity docs

The overriding function may only change the visibility of the overridden function from external to public. The mutability may be changed to a more strict one following the order: nonpayable can be overridden by view and pure. view can be overridden by pure. payable is an exception and cannot be changed to any other mutability.

Now check out this example, also provided in the documentation:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Base
{
    function foo() virtual external view {}
}

contract Middle is Base {}

contract Inherited is Middle
{
    function foo() override public pure {}
}

This contract compiles completely fine since the view function was made into a more strict pure function.

Now if we take the same example, but try to take away the view keyword, we get this error on compile:

TypeError: Overriding function changes state mutability from "view" to "nonpayable".

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Base
{
    function foo() virtual external view {}
}

contract Middle is Base {}

contract Inherited is Middle
{
    function foo() override public {}
}

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.