solidity
solidity copied to clipboard
Access to constants through contract names
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;
}
}
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.
Now that we have the pure check, it should be fine to do this at least for actually pure constants.
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.
I am leaning towards that constants should be supported in interfaces instead.
It seems this is solved with Solidity 0.6
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?
Ah sorry, I thought I tried the example, but I must have made a mistake.
can confirm, 100% still an issue
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.
Just for reference - issue about constants at file level: #9671.
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).
I also believe that something like this would be perfectly possible
type(ContractWithConstant).CONSTANT_NAME
very similar to
type(uint).max
This issue has been marked as stale due to inactivity for the last 90 days. It will be automatically closed in 7 days.
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.