Can you provide an example of when and why you would want to define and use a function type, as a opposed to simply calling a function?

I understand how from here: https://ethereum.stackexchange.com/a/32464/44907

I just don't understand why.

1 Answer 1


The answer is why not?

IMHO it is not a feature that is really needed for smart contract, but it doesn't hurt to have it available.

For example it is possible to implement a kind of dispatch. (If you look at magic implementation it doesn't have an if).

pragma solidity ^0.4.25;

contract Foo {
    mapping (address => function (uint, uint) internal pure returns (uint)) k;

    event New(uint val);

    function gold(uint a, uint b) internal pure returns (uint) {
        return a + b * 10;

    function silver(uint a, uint b) internal pure returns (uint) {
        return a + b * 5;

    function eval(
        function (uint, uint) internal pure returns (uint) f,
        uint a,
        uint b) internal pure returns (uint)
        return f(a, b);

    function register() public payable {
        k[msg.sender] = msg.value > 31415 ? gold : silver;

    function magic(uint a, uint b) public {
        uint val = eval(k[msg.sender], a, b);
        emit New(val);
  • Awesome example! You could simplify this by just doing k[msg.sender](a, b) but being able to create a mapping that includes a function feels very powerful. :thumbsup:
    – valem
    Commented Nov 21, 2018 at 23:58

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.