3

this:

    mapping(uint => function()) funcMap;

doesn't give any error in Remix.

But I don't know how to call it:

pragma solidity ^0.5.2;
contract b {
    mapping(uint => function()) funcMap;
    function g() public view {
        uint x=0;
    }
    function update() public {
        funcMap[1] = g();
    }

this line

        funcMap[1] = g();

gives this error:

browser/Playground.sol:14:22: TypeError: Type tuple() is not implicitly convertible to expected type function ().
    funcMap[1] = g();
                 ^-^
3

Remove parentheses of g function from funcMap[1] = g(); and set funcMap[1] = g;.

look this code run without error:

pragma solidity ^0.5.2;
contract b {
    mapping(uint => function()) funcMap;

    function g() public pure {
        uint x=0;
    }

    function update() public {
        funcMap[1] = g;
    }

}
  • It did solve the problem, thanks. But now when I try to return a uint in g(), it shows an error. It says: (line of funcMap[1] = g;):(error points to '['): TypeError: Type function () view returns (uint256) is not implicitly convertible to expected type function (). – Ornataweaver Dec 29 '18 at 7:37
  • If your function returning uint256 value then your mapping should be like this mapping(uint => uint256) funcMap; and you assign it like funcMap[1] = g(); – Mahesh Rajput Dec 29 '18 at 8:40
  • @Ornataweaver Try mapping(uint => function() returns (uint)) funcMap; and function g() public pure returns (uint) { return 33; }. – Ismael Dec 29 '18 at 16:16

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.