Is it possible to call function in the same contract based on function signature?

Is it possible to do something like this without writing a lot of if-else:

contract {
  function func1(bytes4 sig) public {
      // call its function whose signature is sig
  }

  // signatur == sig
  function func2() {
     ...
  }
}

Thank you guys!

Have you considered using function pointers instead of signatures?

contract C {
    function func1(function () external _f) public {
        _f();
    }

    function func2() public {
        ...
    }
}

And they’re actually just an address+signature under the hood, just packaged in a more type-safe way.

1 Like