Public view functions on the other hand are very common, since reading public storage does not require any state
modifications. These are essentially equivalent to a Solidity view function.
No compile time checks are performed on whether a function can be made view. If a function marked as view
attempts to modify state, that will result in runtime failures.
View functions cannot modify state in any way, including performing contract calls that would in turn modify state.
This makes them easy to reason about: they are simply 'pure' functions that return a value, and carry e.g. no reentrancy risk.
Use Cases
Only public and private functions can be
view.Private
viewfunctions are typically not very useful as they cannot emit nullifiers, which is often required when reading private state (e.g. from acrate::state_vars::private_mutable::PrivateMutableorcrate::state_vars::private_set::PrivateSetstate variable). They do however have their use cases, such as performing acrate::state_vars::delayed_public_mutable::DelayedPublicMutableread.Public view functions on the other hand are very common, since reading public storage does not require any state modifications. These are essentially equivalent to a Solidity
viewfunction.Guarantees
viewfunctions can only be called in a static execution context, which is typically achieved by calling thecrate::contract_self::ContractSelfPublic::viewmethod onself.No compile time checks are performed on whether a function can be made
view. If a function marked as view attempts to modify state, that will result in runtime failures.