Implementing Contract Allows for Lack of Return

I have an abstract contract that requires a contract to implement a function returning a bool

pragma solidity ^0.8.17;

abstract contract Lockable {
    function lock() public virtual returns (bool);
}

But when I leave out the return in the implementing contract, the contract compiles and I'm able to deploy it.

contract Lock is Lockable {
    function lock() public override returns (bool) {
    }
}

I'm not able to compile an implementation that explicitly returns the wrong type, which is an expected behavior.

contract Lock is Lockable {
    function lock() public override returns (bool) {
        return "not a a bool";
    }
}

Why am I allowed to not provide a return? If anyone has a link to a doc on this behavior feel free to share.

Hi,

It is something solidity allows to do, if there isn't a return statement it returns the default for that value, in the case of bool it is false, but if you try to return the wrong value y fails the cast. Check out the PR that introduced that change in solidity: https://github.com/ethereum/solidity/issues/4751

1 Like