solidity icon indicating copy to clipboard operation
solidity copied to clipboard

Access to constants through contract names

Open ethernomad opened this issue 7 years ago • 11 comments

It is currently possible to do this:

pragma solidity ^0.4.3;

contract Constants {

    uint constant FLAG = 0x1;
}

contract Test is Constants {

    function x() returns (uint) {
        return Constants.FLAG;
    }
}

But surely it should be possible to do this:

pragma solidity ^0.4.3;

contract Constants {

    uint constant FLAG = 0x1;
}

contract Test {

    function x() returns (uint) {
        return Constants.FLAG;
    }
}

ethernomad avatar Oct 26 '16 09:10 ethernomad

We still have to figure out how we want constants to behave exactly. Currently, I think you might be able to access private information via constants, which is perhaps not such a good idea.

chriseth avatar Oct 26 '16 12:10 chriseth

Now that we have the pure check, it should be fine to do this at least for actually pure constants.

chriseth avatar Mar 17 '17 16:03 chriseth

As a side note to this ticket: To simulate the desired constant behavior until it's fully supported in the language I'm using a workaround via libraries:

library Constants {
   function FLAG() pure returns (uint) {
      return 0x1;
   }
}

contract Test {
    function x() returns (uint) {
        return Constants.FLAG();
    }
}

To be frank, I actually don't know the cost of simulating constants via library functions, because you have an additional function on the callstack and memory every time you access it, but it does the trick and gives me what I need for more efficient coding. Once constants are supported fully, it is not too hard to turn the library into a contract and search/replace its usage to remove the () parentheses.

I'm not worried about gas cost as the solutions that use this approach run on a permissioned EVM with no gas limit, but it would be great to get your opinion on the above, @chriseth.

j-h-scheufen avatar Mar 09 '18 13:03 j-h-scheufen

I am leaning towards that constants should be supported in interfaces instead.

axic avatar Mar 21 '19 22:03 axic

It seems this is solved with Solidity 0.6

cygnusv avatar Apr 06 '20 20:04 cygnusv

I looked at the Solidity 0.6 release notes and did not see how this is solved with that release. Can anyone point to an example or other relevant docs?

j-h-scheufen avatar Apr 08 '20 14:04 j-h-scheufen

Ah sorry, I thought I tried the example, but I must have made a mistake.

chriseth avatar Apr 15 '20 10:04 chriseth

can confirm, 100% still an issue

sambacha avatar May 26 '20 21:05 sambacha

Design call: moving it into the icebox because there seems to be not too much interest. We may allow constants at file level in the future instead.

chriseth avatar Jun 24 '20 12:06 chriseth

Just for reference - issue about constants at file level: #9671.

cameel avatar Sep 03 '20 15:09 cameel

This is still an issue on 0.8.15:

Contract Test1 {
    uint256 public constant WAD = 1e18;
}

Contract Test2 {
    function getWad() external pure returns (uint256) {
        return Test1.WAD();
    }
}

returns Member "WAD" not found or not visible after argument-dependent lookup in type(contract Test1).

pedrommaiaa avatar Jul 29 '22 14:07 pedrommaiaa

I also believe that something like this would be perfectly possible

type(ContractWithConstant).CONSTANT_NAME

very similar to

type(uint).max

itaborda avatar Sep 28 '22 00:09 itaborda

This issue has been marked as stale due to inactivity for the last 90 days. It will be automatically closed in 7 days.

github-actions[bot] avatar Feb 12 '23 12:02 github-actions[bot]

Hi everyone! This issue has been automatically closed due to inactivity. If you think this issue is still relevant in the latest Solidity version and you have something to contribute, feel free to reopen. However, unless the issue is a concrete proposal that can be implemented, we recommend starting a language discussion on the forum instead.

github-actions[bot] avatar Feb 20 '23 12:02 github-actions[bot]