Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the definition of the Solidity syntax constant/view/pure keyword

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the definition of constant/view/pure keyword in Solidity grammar". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the definition of constant/view/pure keyword in Solidity grammar".

1 summary of differences

The function of the three constant,view,pure function modifiers in Solidity is to tell the compiler that the function does not change / read state variables, so that function execution does not consume gas (no consumption at all! ), because miners are not needed to verify it.

Before Solidity v4.17, there was only constant. Later, someone thought that the word constant itself represented a constant in a variable, which was not suitable for modifying a function, so constant was split into view and pure.

The function of view is exactly the same as that of constant. It can read state variables but cannot change them.

Pure is more stringent, pure modified functions can not be changed and can not read state variables, otherwise the compilation will not pass.

You can run the following test code to deepen your understanding of these three keywords.

Contract constantViewPure {string name; uint public age; function constantViewPure () public {name = "liushiming"; age = 29;} function getAgeByConstant () public constant returns (uint) {age + = 1; / / declared as constant, and trying to change the value of the state variable in the function body, the compiler will report warning, but you can use return age; / / return 30, but! The value of the state variable age will not change, still 29! } function getAgeByView () public view returns (uint) {age + = 1; / / view has the same effect as constant. Compilation will report warning, but it can be done through return age; / / return 30, but! The value of the state variable age will not change, still 29! } function getAgeByPure () public pure returns (uint) {return age; / / compile error! Pure is stricter than constant and view, and pure completely forbids reading and writing state variables! Return 1;}} 2 detailed description

2.1 Constant state variables

State variables can be declared as constant. In this case, only expressions that have a definite value at compile time can be used to assign values to them. Any assignment to storage, blockchain data (such as now, this.balance, or block.number) or execution data (msg.gas) or calls to external contracts is not allowed. Expressions that have a side-effect effect on memory allocation are allowed, but expressions that have a boundary effect on other memory objects are not. The built-in (built-in) functions keccak256,sha256,ripemd160,ecrecover,addmod and mulmod are allowed (even if they do call external contracts).

The reason for allowing memory allocators with boundary effects is that it will allow you to build complex objects, such as lookup-table. This feature is not yet fully available.

The compiler does not reserve storage for these variables, and each occurrence of them is replaced with a corresponding constant expression (which may be evaluated by the optimizer to an actual value).

Not all types of state variables can be decorated with constant. Currently, only value types and strings are supported.

Pragma solidity ^ 0.4.0X contract C {uint constant x = 32 percent contract 22 + 8; string constant text = "abc"; bytes32 constant myHash = keccak256 ("abc");}

2.2 View function

The function can be declared as a view type, in which case the state is not modified.

The following statement is considered to be a modified state:

Modify the status variable.

Generate events (https://solidity-cn.readthedocs.io/zh/develop/contracts.html?highlight=view#events).

Create another contract (https://solidity-cn.readthedocs.io/zh/develop/control-structures.html#creating-contracts).

Use selfdestruct.

Send the etheric coin through the call.

Call any function that is not marked view or pure.

Use low-level calls.

Use inline assembly that contains specific opcodes.

Pragma solidity ^ 0.4.16 X contract C {function f (uint a, uint b) public view returns (uint) {return a * (b + 42) + now;}}

Notes:

Onstant is an alias for view.

The Getter method is marked view.

The compiler does not force the view method not to modify the state.

2.3 Pure function

The function can be declared as pure, in which case it promises not to read or modify the state.

In addition to the list of state modification statements explained above, the following is considered to be read from the state:

Read the status variable.

Access this.balance or .balance.

Access block,tx, any member of msg (except msg.sig and msg.data).

Call any function that is not marked pure.

Use inline assembly that contains some opcodes.

Pragma solidity ^ 0.4.16 X contract C {function f (uint a, uint b) public pure returns (uint) {return a * (b + 42);}}

Warning

The compiler does not force the pure method not to read the state.

Thank you for your reading, the above is the content of "what is the definition of constant/view/pure keyword in Solidity grammar". After the study of this article, I believe you have a deeper understanding of what the definition of constant/view/pure keyword in Solidity grammar is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 206

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report