0

"If a function type variable is not initialised, calling it results in a failed assertion. The same happens if you call a function after using delete on it." is a quote from the official Solidity documentation on Function Types

How do you "use delete on a function", though? 🤔

2 Answers 2

2

You cannot delete a function. What you can delete, though, is a variable that points to a function.

The documentation, likely, meant to say "The same happens if you call a function after using delete on a Function Type variable that points to that function".

Here's an example contract illustrating this scenario:

contract Test {
    // Variable pointing to a function
    function() internal pure pFunc;

    function foo() internal pure { }

    function das() public {
        // Initialize the function pointer
        pFunc = foo;

        // Execute the function via the function pointer
        pFunc();

        // Delete the function pointer variable
        delete pFunc;

        // Call the deleted function pointer (results in the tx being reverted)
        // If the line below is commented out, however, the function executes alright
        pFunc();
    }
}
3
  • Right... But, getting back to the quote from the documentation in light of what you said: how do you "call a function after using delete on it"?..
    – Iaroslav
    May 11, 2023 at 8:48
  • 2
    @Iaroslav You could write pFunc() but it will fail because it no longer point to a valid function.
    – Ismael
    May 11, 2023 at 16:32
  • Thanks for the extra details! I've accepted your answer and edited it to better align it with the question.
    – Iaroslav
    May 11, 2023 at 19:22
1

In Solidity, the delete keyword is used to reset or clear the state of a variable or an array. However, it does not have any effect on the functions themselves.

1
  • So, is the official documentation wrong?
    – Iaroslav
    May 8, 2023 at 18:24

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.