Wording about function types in documentation

Link

“Variables of function type can be assigned from functions and function parameters of function type can be used to pass functions to and return functions from function calls.”

Could this potentially be made clearer? Are functions considered first-class citizens in Solidity?

Explaining by example:

Function type:

function (uint16, uint32) view returns (uint64)

Variables of function type can be assigned from functions:

function add(uint16 x, uint32 y) internal pure returns (uint64) {
    return uint64(x) + uint64(y);
}
function mul(uint16 x, uint32 y) internal pure returns (uint64) {
    return uint64(x) * uint64(y);
}
function runFunc(bool flag) external pure returns (uint64) {
    function (uint16, uint32) view returns (uint64) func = flag ? add : mul;
    return func(x, y);
}

Function parameters of function type can be used for passing functions to functions:

function receiveFunc(function (uint16, uint32) view returns (uint64) func)
external pure returns (bool) {
    if (func == add)
        return true;
    else
        return false;
}

Function parameters of function type can be used for returning functions from functions:

function returnFunc(bool flag)
external pure returns (function (uint16, uint32) view returns (uint64)) {
    if (flag)
        return add;
    else
        return mul;
}
1 Like

Thank you! This is much clearer.

1 Like