0

I've seen interfaces in which the function definitions specify a return type, ie: (from openZepplin's IERC721.sol)

function balanceOf(address owner) public view returns (uint256 balance);

and sometimes, even in the same interface, you'll see something like:

function approve(address to, uint256 tokenId) public;

Obviously, it doesn't make sense for approve to return a uint. It could, however, return a boolean value:

return(bool success)

I can see that the absence of a return type in the interface allows more flexibility in how the implementation handles the return value, but are there any other drawbacks/benefits to specifying a return type vs leaving it out of the interface?

0

Watch what happens if they don't match:

pragma solidity 0.4.25;

contract ApproveInterface {
    function approve(address to, uint256 tokenId) public;
}

contract Approve is ApproveInterface {
    function approve(address to, uint256 tokenId) public returns(bool success) {
        return true;
    }
}

Error:

browser/Interface1.sol:8:5: TypeError: Overriding function return types differ.
    function approve(address to, uint256 tokenId) public returns(bool success) {
    ^ (Relevant source part starts here and spans across multiple lines).
browser/Interface1.sol:4:5: Overriden function is here:
    function approve(address to, uint256 tokenId) public;
    ^---------------------------------------------------^

You can define a different function signature that uses different arguments (the function signature is based on a hash) and that will be treated as a completely different function. The compiler doesn't complain.

pragma solidity 0.4.25;

contract ApproveInterface {
    function approve(address to, uint256 tokenId) public;
}

contract Approve is ApproveInterface {
    uint x;
    function approve(address to, uint256 tokenId, bytes32 other) public {
        x = 1;
    }
}

However, it isn't deployable because it's treated as a completely separate function that doesn't override the empty function in the interface. Consequently, the contract has inherited a still-undefined transaction from the interface and therefore cannot be deployed.

To see what I mean, try deploying it.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.