Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: Initializing constants with pure functions should be possible #13232

Closed
migoldfinger opened this issue Jul 1, 2022 · 2 comments
Closed
Labels
language design :rage4: Any changes to the language, e.g. new features

Comments

@migoldfinger
Copy link

Abstract

At the moment it is impossible to use pure functions to initialize constants.

Motivation

Pure functions return based on the parameters if any always the same value regardless of time or status of the contract. In a way they are acting like static functions in C# for example.
Therefore it should be possible to use pure functions to initialize constant variables.

Specification

For gas optimization the compiler should calculate the return value of a pure function used for constant initialization and replace the call with the returned value. Thinking ahead the compiler should do that with the complete expression right of the = operator if not already the case.

Backwards Compatibility

There should be no breaking changes.

Example

contract TestContract
{
	uint256 private constant _MAX_SUPPLY = (1000000000 * (10**decimals()));

	function decimals() override public pure virtual returns (uint8)
	{
		return 18;
	}
}
@hrkrshnn
Copy link
Member

hrkrshnn commented Jul 1, 2022

In theory, this is great. Unfortunately, our current definition of pure is rather broad. For example, you can have external calls in pure functions! That already makes constants incompatible with pure.

@cameel cameel moved this to Triage in Solidity Focus Board Jul 4, 2022
@nishant-sachdeva nishant-sachdeva moved this from Triage to Design/Decide in Solidity Focus Board Jul 15, 2022
@cameel
Copy link
Member

cameel commented Jul 19, 2022

Duplicate of #3122.

For gas optimization the compiler should calculate the return value of a pure function used for constant initialization and replace the call with the returned value.

You're thinking of pure as something that has zero side-effects but that's not the case in Solidity. Currently it only means that the function cannot read and write storage. It can still freely read and write memory and calldata, use codecopy or, as mentioned by @hrkrshnn, even make external calls. We have a whole discussion about it (#8153), but at the moment I think it's unlikely that we'll change it to the point of making it mean "side-effect free". It's more likely that we'll just introduce a new visibility level for such functions. That's actually something we have on our roadmap for the near future (#3157).

@cameel cameel closed this as not planned Won't fix, can't repro, duplicate, stale Jul 19, 2022
Repository owner moved this from Design/Decide to Done in Solidity Focus Board Jul 19, 2022
@cameel cameel added feature language design :rage4: Any changes to the language, e.g. new features labels Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language design :rage4: Any changes to the language, e.g. new features
Projects
None yet
Development

No branches or pull requests

3 participants