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

How solidity expressions and control structures are used

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to use solidity expressions and control structures". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Expression and control structure control structure

Except for switch and goto,solidity, most of the control structures are derived from C / JavaScript,if, else, while, for, break, continue, return,?:, which have the same semantics as C / JavaScript.

Parentheses in conditional statements cannot be omitted, but curly braces before and after a single statement can be omitted.

Note that (in Solidity) there is no conversion from a non-Boolean type to a Boolean type like C and JavaScrip, so if (1) {... } is not a legal Solidity (statement).

Function call

Internal function call

The current contract and function can be called directly ("internal") or recursive, so you will see in this "ridiculous" example:

Contract c {function g (uint a) returns (uint ret) {return f ();} function f () returns (uint ret) {return g (7) + f ();}}

These function calls are translated into simple jumps statements in EMV. The current memory is not cleared, that is, functions referenced by memory are very efficient. Only functions with the same contract can be called internally.

External function call

The expression this.g (8); is also a legitimate function call, but this time, the function will be called "external" and will be called through a message rather than directly through jumps. The functions of other contracts are also external calls. For external calls, all function parameters must be copied into memory.

When other contract functions are called, the number of wei for this call is sent and the gas is defined:

Contract InfoFeed {function info () returns (uint ret) {return 42;}} contract Consumer {InfoFeed feed; function setFeed (address addr) {feed = InfoFeed (addr);} function callFeed () {feed.info.value (10) .gas (800) ();}

Note: the expression InfoFeed (addr) performs an explicit type conversion, meaning "We know that the contract type of the given address is InfoFeed", which does not execute the constructor. We can also use the function setFeed (InfoFeed _ feed) {feed = _ feed;} directly. Note: feed.info.value (10) .gas (800) is the (local) setting and the number of gas sent by the function call, and the actual call is completed only after the end of the last parenthesis.

Named calls and anonymous function parameters

Function calls with parameters can have a name, and names that do not use parameters (especially return parameters) can be omitted.

Contract c {function f (uint key, uint value) {...} function g () {/ / named arguments named parameter f ({value: 2, key: 3});} / omitted parameters parameter function func (uint k, uint) returns (uint) {return k;}} the order in which expressions are evaluated

The order in which expressions are evaluated is uncertain (to be exact, the order in which child node expressions in the sequential expression tree are evaluated is uncertain, but for the nodes themselves, the order in which expressions are evaluated is of course determined). Only the order in which the statements are executed is guaranteed, as well as the short-circuit rules for Boolean expressions.

Assignment

Destruct assignment and return multiple values

Solidity internally allows tuple types, that is, the size of a series of different types of objects to be constant at compile time. These tuples can be used to return multiple values at the same time and assign them to multiple variables (or left-value operations) at the same time.

Contract C {uint [] data; function f () returns (uint, bool, uint) {return (7, true, 2);} function g () {/ / Declares and assigns the variables. Specifying the type explicitly is not possible. Var (x, b, y) = f (); / / Assigns to a pre-existing variable. (X, y) = (2,7); / / Common trick to swap values-- does not work for non-value storage types. (X, y) = (y, x); / / Components can be left out (also for variable declarations). / / If the tuple ends in an empty component, / / the rest of the values are discarded. (data.length,) = f (); / / Sets the length to 7 / / The same can be done on the left side. (, data [3]) = f (); / / Sets data [3] to 2 / / Components can only be left out at the left-hand-side of assignments, with / / one exception: (X,) = (1,) is the only way to specify a 1-component tuple, because (1) is / / equivalent to 1.}} contract C {uint [] data Function f () returns (uint, bool, uint) {return (7, true, 2);} function g () {/ / Declares and assigns the variables. Specifying the type explicitly is not possible. Declare and assign variables without showing the definition type var (x, b, y) = f (); / / Assigns to a pre-existing variable. Assign to the existing variable (x, y) = (2,7); / / Common trick to swap values-- does not work for non-value storage types. Tips for exchanging values-does not work for non-value storage types (x, y) = (y, x); / / Components can be left out (also for variable declarations). Elements can be excluded (also applicable for variable declarations) / / If the tuple ends in an empty component, if the tuple ends with an empty element / / the rest of the values are discarded. The rest of the value is discarded (data.length,) = f (); / / Sets the length to 7 sets the length to 7 / / The same can be done on the left side. You can also do (, data [3]) = f () on the left side; / / Sets data [3] to 2 sets data [3] to 2 / / Components can only be left out at the left-hand-side of assignments, with / / one exception: components can only be excluded on the left side of the assignment, with one exception (x,) = (1,) / / (1,) is the only way to specify a 1-component tuple, because (1) is (1,) is a tuple that defines an element, (1) is a combination of an array and structure equal to 1 / / equivalent to 1.}}

For non-value types such as arrays and structs, the semantics of assignments are more complex. Assigning a value to a state variable always requires the creation of a separate copy. For basic types, on the other hand, assigning to a local variable requires the creation of a separate copy, a 32-byte static type. If a structure or array (including bytes and strings) is assigned from a state variable to a local variable, the local variable holds a reference to the original state variable. The second assignment to a local variable does not change the state, but only changes the reference. Members (or elements) assigned to local variables change the state.

Abnormal

There are some situations where exceptions are automatically thrown (see below). You can use the throw directive to manually throw an exception. The effect of the exception is that the currently executed call is stopped and resumed (that is, no changes in state and balance have occurred). In addition, exceptions can also be "popped up" through the Solidity function (once an "exception" occurs, send "exceptions", and the underlying functions of call and callcode return false).

It is impossible to catch an exception.

In the next example, we will demonstrate how to easily restore an Ether transfer and how to check the return value of send:

Contract Sharer {function sendHalf (address addr) returns (uint balance) {if (! addr.send (msg.value/2)) throw; / / also reverts the transfer to Sharer return this.balance;}} contract Sharer {function sendHalf (address addr) returns (uint balance) {if (! addr.send (msg.value/2)) throw / / also reverts the transfer to Sharer also resumes the transfer return this.balance; of Sharer}}

Currently, Solidity exceptions occur automatically. There are three situations:

If you access an array that exceeds its length (i.e. x [I] where I > = x.length)

If a function called through a message does not end correctly (that is, it runs out of gas, or throws an exception itself).

If a function that does not exist in a library is called, or Ether is sent to a library.

Internally, when an exception is thrown, Solidity executes an "illegal jump", causing the EMV (Ether virtual machine) to restore all changed states. The reason for this is that there is no secure way to continue execution, and the expected results do not occur. Because we want to preserve the atomicity of the transaction, the safest thing to do is to restore all changes and leave the entire transaction (or at least invocation) unaffected.

That's all for "how solidity expressions and control structures are used". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 0

*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