You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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).
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
The text was updated successfully, but these errors were encountered: