Vyper: VIP: Add keyword for immutable state variables

Created on 30 Dec 2018  路  6Comments  路  Source: vyperlang/vyper

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

  • final
  • immutable
  • frozen
  • ... or whatever

Specification

If the keyword is final, then it can be used like this:

# Auction params
# Beneficiary receives money from the highest bidder
beneficiary: public(final(address))
auctionStart: final(public(timestamp))
auctionEnd: public(final(timestamp))

Immutability is checked only in compile time

Backwards Compatibility

None

Copyright

Copyright and related rights waived via CC0

Easy Pickings Approved

All 6 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 final constants to not have to be set/accessed with the self.CONSTANT syntax so it fits in the same way as we use constants (only set at deployment instead of in code)

Just a note: Marking something as final means it can only be set in __init__ which also means we need to warn that a variables is set to default/zero if not assigned in __init__ (the alternative to the __init__limitation being a check for 1 assignment maximum).

Should the compiler throw an Error if it's not set in __init__? I would argue that's unintended behavior and they should either set it or make it a constant.

Yes, this would definitely have to be an error as it violates final.

Approved :+1:

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.

Was this page helpful?
0 / 5 - 0 ratings