-
-
Notifications
You must be signed in to change notification settings - Fork 812
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
VIP: Add keyword for immutable state variables #1164
Comments
I like this proposal. It works for the concept of deployment parameters that is a pretty common usecase (for things like the ticker symbol of a token, withdrawal period of Plasma, etc) As noted, it's basically a hint for auditing and a compile-time check, so not hugely necessary. Maybe we could allow |
Just a note: Marking something as final means it can only be set in |
Should the compiler throw an Error if it's not set in |
Yes, this would definitely have to be an error as it violates |
Approved 👍 |
for gas savings, we can skip SLOADing immutable variables. in the constructor, we can set the values of the immutable variables in a data section of the runtime code and then at runtime use CODECOPY to load the values. |
I would have expected this functionality to be implemented as part of |
A constant gets injected directly into the bytecode during compilation. The suggestion here would be a state variable that can only be set once (during It would still be a storage variable. Dynamically assembling the bytecode during deployments is a really difficult exercise, but could be done as an optimization to further reduce code size/gas cost. |
Right. Thanks for the low-level hints.
I was thinking that one could have some sort of templating similar to say
and at compile time the value of This way, it is still a constant at compile time and we have the gas optimizations as well as relatively clean code. |
I had a similar idea too, I think it would be useful but IMO that's a new VIP. In general, I want to support using Vyper as a templating tool too. For example, in VERSION = public(constant(String[20])) = {{ $VERSION }}
BENEFICIARY: public(constant(address)) = {{ $BENEFICIARY }} that would be pretty cool |
note for those looking at this issue for implementation details: in the current implementation (v0.3.3), immutables are appended to the runtime code during deployment, there is no storage overhead. |
Simple Summary
Add a keyword for state variables of a contract that ensures immutability
Abstract & Motivation
Data marked with this keyword must be set in
__init__
This keyword will make code more readable, thus making it more secure.
The keyword might be
Specification
If the keyword is
final
, then it can be used like this:Immutability is checked only in compile time
Backwards Compatibility
None
Copyright
Copyright and related rights waived via CC0
The text was updated successfully, but these errors were encountered: