2

It seems all function definitions in an interface must be external. However is it okay to define a public function in its interface as external? If so, how can I define a public function that takes a bytes memory?

for example, How could I write an interface for this contract:

contract C {
  function a(bytes memory b) public view returns (bool) {
    return true;
  }
}
Jonah
  • 655
  • 1
  • 7
  • 17

2 Answers2

2

However is it okay to define a public function in its interface as external?

No, you cannot do that in an interface. As per the Solidity documentation:

Interfaces are similar to abstract contracts, but they cannot have any functions implemented. There are further restrictions:

  • ...
  • All declared functions must be external.
  • ...

But what you can do is define your interface like this:

interface MyInterface {
  function foo(bytes calldata b) external view returns (bool);
}

And your contract like this:

import "./MyInterface.sol";

contract MyContract is MyInterface { function foo(bytes memory b) public view returns (bool) { ... } }

Note: there are two additional parameters, virtual and override, to be cognizant of if you're using Solidity v6 or above. Read this thread for more info.

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143
1

This should work:

interface C {
  function a(bytes calldata b) external view returns (bool);
}

A public function can handle being called from inside the contract or outside, so it sort of has two interfaces. The internal one uses memory for the parameters' location, but the external interface uses call data.

user19510
  • 27,999
  • 2
  • 30
  • 48