Solidity 0.4.16 introduced the
constant function modifiers. The documentation says:
constantfor functions: Same as
Does this mean
view is just an alias for
constant? If so, why do we need it?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
As I understand it, the point is that
constant is misleading (constant functions don't necessarily return constant results), and not particularly nuanced. The changes when complete will introduce two replacement keywords:
pure which are intended to be more meaningful and useful.
From the GitHub issue,
- constant function should not modify the state (not fully enforced yet)
- constant state variable (ie. the one in the class and not in a method) is evaluated every time it is called
After the change:
- the keyword view is introduced for functions (it replaces constant). Calling a view cannot alter the behaviour of future interactions with any contract. This means such functions cannot use SSTORE, cannot send or receive ether and can only call other view or pure functions.
- the keyword pure is introduced for functions, they are view functions with the additional restriction that their value only depends on the function arguments. This means they cannot use SSTORE, SLOAD, cannot send or receive ether, cannot use msg or block and can only call other pure functions.
- the keyword constant is invalid on functions
- the keyword constant on any variable means it cannot be modified (and could be placed into memory or bytecode by the optimiser)
Functions are declarable as view, making them promise not to modify the state, thus only being used for viewing the state.
Here’s a list of conditions for a statement to be considered as “modifying the state”: