The current Solidity Chinese document is either too poorly translated or too old. Decided to re-translate it. Specifically naming and criticising the Geek Academy’s translation of the Official Solidity Document Chinese, which is even better than that in machine translations, people should stop looking at it.

Writing in the front

Solidity is the ethereum smart contract programming language. Before reading this article, you should know something about Ethereum and smart contracts. If you don’t already, you are advised to read what Ethereum is.

The first half of this article is translated with reference to the official Solidity document (current version: 0.4.20) and the second half illustrates the use of types with actual contract code examples (for column subscribers only).

type

Solidity is a statically typed language, meaning that each variable (local or state variables) needs to specify the type of the variable at compile time (or at least pull out the type). Solidity provides basic types that can be combined into complex types.

Solidity types fall into two categories:

  • Value Type – A variable is always copied in Value assignment or parameter passing.
  • Reference Types

Value Type

Value types include:

  • Booleans
  • Integer (Integers)
  • Fixed Point Numbers
  • Fixed-size byte Arrays
  • Rational numbers and Integer Constants (Rational and Integer Literals)
  • String literals
  • Hexadecimal constants (Hexadecimal literals)
  • Enumeration (Enums)
  • Function (Function Types)
  • Address (Address)
  • Address Literals

    Functions and addresses will be explained in a later article.

Booleans

Boolean (bool): The possible values are constant values true and false.

Boolean types support the following operators:

  • ! Logic is not
  • With && logic
  • | | logic or
  • = = is equal to the
  • ! = is not equal to

Note: operators && and | | is short-circuit operator, such as f (x) | | g (y), when f (x) is true, will not continue to execute g (y).

Integer (Integers)

Int /uint: indicates a signed and unsigned integer with different digits. The keywords uint8 to uint256 are supported (in 8 steps), with uint and int corresponding to uint256 and int256 by default.

Supported operators:

  • Comparison operators: <=, <, ==,! =, >=, > (Returns Boolean: true or false)
  • Operator: & |, ^ (exclusive or), ~ (DE) a
  • Arithmetic operators: +, -, unary -, unary +,, /, %(remainder), **(power), << (left shift), >>(right shift)

Description:

  1. Integer division is always truncated, but not if the operator is literal (literals come later).
  2. An integer divided by 0 throws an exception.
  3. The result of the shift operation depends on the number to the left of the operator. X << y and x2 * *Y is equal, x >> y is equal to x / 2**y.
  4. There can be no negative shift, that is, the number to the right of the operator cannot be negative, or a runtime exception will be thrown.

Note: in Solidity right shifts are equivalent to division, so a negative right shift will be 0 when rounded down, not infinitely negative decimals as in other languages.

Fixed Point Numbers

Note: fixed-length floating-point Solidity is not fully supported, it can be used for declaring variables but not for assigning values.

Fixed/uFIXED: fixed bit floating point numbers with and without signs. The keyword is ufixedMxN and ufixedMxN. M represents the number of bits to be occupied by this type, in 8 steps, ranging from 8 to 256 bits. N indicates the number of decimal points, which can be between 0 and 80

Supported operators:

  • Comparison operators: <=, <, ==,! =, >=, > (Returns Boolean: true or false)
  • Arithmetic operators: +, -, unary -, unary +,, /, %(remainder)

    Note: Unlike float and double in most languages,*M
    Represents the fixed number of digits occupied by the whole number, including the integer part and the fractional part. So when a floating-point number is represented by a small digit (M is small), the decimal part takes up almost the entire space.

Fixed-size byte Arrays

Keywords: bytes1, bytes2, bytes3… , bytes32. Byte represents bytes1 (incremented by step 1).

Supported operators:

  • Comparators: <=, <, ==,! =, >=, > (Mandatory bool)
  • Operator: |, &, ^ (the bitwise xor), ~ (by invert), < < displacement (left), > > (right)
  • Index (subscript) access: if x is bytesI, x[k] returns the KTH byte (read only) when 0 <= k < I.

A shift is similar to an integer in that the result of a shift is positive or negative depending on the number to the left of the operator and cannot be negatively shifted.

Member variable:.length: represents the length of this byte array (read only).

Dynamically sized (Dynamically allocated) Byte arrays

  • Bytes: Dynamically allocated size byte Arrays, see Arrays, not value types!
  • String: Dynamically allocated size UTF8 encoded character type. For details, see Arrays. Not a value type!

As a rule of thumb: bytes are used to store arbitrarily long byte data, and strings are used to store arbitrarily long (UTF-8 encoded) string data. If the length can be determined, try to use a fixed length such as byte1 to byte32, because it saves space.

Rational numbers and Integer Constants (Rational and Integer Literals)

Some people also translate it literally

Integer constants are composed of a series of numbers from 0 to 9, expressed in base 10, e.g., base 8 is not available and prefacing 0 is invalid in Solidity.

Decimal Fraction Literals takes a., in. There must be at least one number on either side of the. Valid expressions are :1.,.1 and 1.3.

Scientific notation also supports that the base can be a decimal and the exponent must be an integer, valid representations such as: 2e10, -2e10, 2E-10, 2.5E1.

Numeric constant expressions themselves support arbitrary precision, that is, no overflow, or division truncation. But when it is converted to the corresponding nonconstant type, or when they are evaluated with noninvariants, the accuracy is not guaranteed. For example :(2*800 + 1) – 2*800 results in 1 (uint8 whole class), although the intermediate result already exceeds the computer word length. Also:.5 * 8 results in 4, despite the involvement of non-orthopedics.

Any operator supported by an integer is valid for an integer constant expression as long as the operand is an integer. If two operands are decimals, bitwise operations are not allowed, and exponents cannot be decimals.

Note: Solidity has a numeric constant type for each rational number. Integer constants and rational constants are subordinate to numeric constants. The result of all numeric constant expressions is numeric constant. So 1 + 2 and 2 + 1 belong to the same rational number constant 3

Warning: integer constant division, which was truncated in earlier versions, can now be converted to rational numbers such as 2.5 for 5/2

Note: a numeric constant expression, once it contains a constant expression, is converted to a nonconstant type. The result of the expression in the following code will be considered a rational number:

uint128 a = 1;

Uint128 b = 2.5 + a + 0.5;

Copy the code

The above code will not compile because b will be considered a decimal by the compiler.

String constant

String constants are strings (” foo “or” bar “) enclosed by either single or double quotes. Unlike C, where strings contain terminators, “foo” is only three bytes in size. As with integer constants, string length types can be variable-length. Strings can be converted implicitly to byte1,… Byte32 is also converted to bytes or string if appropriate.

String constants support escape characters such as \n, \xNN, \uNNNN. Where \xNN represents a hexadecimal value that is eventually converted to the appropriate byte. While \uNNNN represents a Unicode encoding value, which is eventually converted to a UTF8 sequence.

Hexadecimal constants (Hexadecimal literals)

A hexadecimal constant, starting with the keyword hex, followed by a string wrapped in single or double quotes that contains a hexadecimal string, such as HEX “001122FF”. Its value will be expressed in binary.

Hexadecimal constants, like string constants, can also be converted to byte arrays.

Enumeration (Enums)

In Solidity, enumerations can be custom typed. It can display conversions to integer conversions, but not implicit conversions. The conversion shown checks the range of values at run time and raises an exception if it does not match. Enumeration types should have at least one member. Here is an example of an enumeration:

Pragma solidity ^ 0.4.0;



contract test {

enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }

ActionChoices choice;

ActionChoices constant defaultChoice = ActionChoices.GoStraight;



function setGoStraight() {

choice = ActionChoices.GoStraight;

}



// Since enum types are not part of the ABI, the signature of "getChoice"

// will automatically be changed to "getChoice() returns (uint8)"

// for all matters external to Solidity. The integer type used is just

// large enough to hold all enum values, i.e. if you have more values,

// `uint16` will be used and so on.

function getChoice() returns (ActionChoices) {

return choice;

}



function getDefaultChoice() returns (uint) {

return uint(defaultChoice);

}

}

Copy the code

The code examples

To illustrate the use of types through contract code examples, subscribe to Blockchain Technology.

Reference documentation

Official Solidity documentation – Type

Blockchain in Simple terms – systematic learning of blockchain to create the best blockchain technology blog


WeChat pay

  • By Tiny Bear
  • Links to this article: Learnblockchain. Cn / 2017/12/05 /…
  • Copyright notice: This article is published under a CC BY-NC-SA 3.0 license. Reprint please indicate the source!