Value types
INT / UINT
uint is short for unsigned integer, and you can choose the size from uint8 to uint256
uint8starts from0to2 ** 8 - 1uint16starts from0to2 ** 16 - 1...uint256starts from0to2 ** 256 - 1
uint8 public u8 = 1;
uint256 public u256 = 456;
uint public u = 123; // uint is short hand for uint256int is short for integer, and you can choose the size from int8 to int256
int8starts from-2 ** 7to2 ** 7 - 1int16starts from-2 ** 15to2 ** 15 - 1...int128starts from-2 ** 127to2 ** 127 - 1int256starts from-2 ** 255to2 ** 255 - 1
int8 public i8 = -1;
int256 public i256 = 456;
int public i = -123; // int is short hand for int256int and uint operators:
Comparisons: <=, <, ==, !=, >=, > (returns
bool)Bit operations: &, |, ^ (bitwise exclusive hoแบทc), ~ (bitwise negation)
Shifts: << (left shift), >> (right shift)
Addition, Subtraction and Multiplication:
+,-,negative -(as insigned integer),*,/,%(modulo),**(exponentiation)
For a type integer variable X, you can use type(X).min and type(X).max to access smallest and biggest value respectively for that type.
BOOL
bool means Boolean and has 2 possible values which are true and false
Operators:
!(logical negation)&&(logical conjunction, โandโ)||(logical disjunction, โorโ)==(equality)!=(inequality)
The operators || and && apply the common short-circuiting rules. This means that in the expression f(x) || g(y), if f(x) evaluates to true, g(y) will not be evaluated even if it may have side-effects.
ADDRESS
addressis a special data type in Solidity that allows storing 20 bytes (size) of the address of an EVM accountaddress payablesimilar toaddressbut adds 2 more methodstransferandsend
BYTES
In Solidity, the byte data type represents a sequence of bytes. Solidity has two types of bytes:
Fixed-size byte arrays
Dynamic-size byte arrays
The bytes keyword in Solidity represents a dynamic array of bytes. Essentially, it is a shorthand for byte[].
Default values
Declared variables without value assignment will have its default values.
CONTRACT
contract is used to declare a contract in Solidity.
contract can also inherit from another contract using the keyword is
ENUM
Enums are one way to create a user-defined type in Solidity. They are explicitly convertible to and from all integer types but implicit conversion is not allowed. The explicit conversion from integer checks at runtime that the value lies inside the range of the enum and causes a Panic error otherwise. Enums require at least one member, and its default value when declared is the first member. Enums cannot have more than 256 members.
The data representation is the same as for enums in C: The options are represented by subsequent unsigned integer values starting from 0.
Using type(NameOfEnum).min and type(NameOfEnum).max you can get the smallest and respectively largest value of the given enum.
TYPE
A user-defined value type allows creating a zero cost abstraction over an elementary value type. This is similar to an alias, but with stricter type requirements.
A user-defined value type is defined using type C is V, where C is the name of the newly introduced type and V has to be a built-in value type (the โunderlying typeโ)
FUNCTION
function keyword is used to declare a function in Solidity.
We can declare a function like below:
Last updated