Solidity tutorial 2 "Basic syntax"

Comments

  • Single-line comments (//)
  • multi-line comments (/.../)
  • triple-slash comments (///)

There is right in front of function declarations is used to introduce NatSpec comments.

Types of Solidity

There are various types of Solidity, but currently implemented types which are the elementary are

  • Booleans (bool)
  • Integer
  • Fixed-length string/byte array (bytes0 to bytes32)

Integer

They are signed and unsigned integers of various bit widths and addresses.

  • int8/uint8 to int256/uint256 in steps of 8 bits
  • uint/int are aliases for uint256/int256
  • 160 bits

Comparisons always yield booleans which can be combined using &&, || , !.

  • <=
  • !=
  • ==

Note that the usual short-circuiting rules apply for && and ||, which means that for expressions of the form (0 < 1 || fun()), the function is actually never called.

If an operator is applied to different types, the compiler tries to implicitly convert one of the operands to the type of the other (the same is true for assignments).

In general, an implicit conversion is possible if it makes sense semantically and no information is lost: uint8 is convertible to uint16 and int120 to int256, but int8 is not convertible to uint256.

Furthermore, unsigned integers can be converted to bytes of the same or larger size, but not vice-versa.
Any type that can be converted to uint160 can also be converted to address.

If the compiler does not allow implicit conversion but you know what you are doing, an explicit type conversion is sometimes possible:

int8 y = -3; 
uint x = uint(y);

At the end of this code snippet, x will have the value 0xfffff..fd (64 hex characters), which is -3 in two's complement representation of 256 bits.

For convenience, it is not always necessary to explicitly specify the type of a variable, the compiler automatically infers it from the type of the first expression that is assigned to the variable:

uint20 x = 0x123;
var y = x;

Here, the type of y will be uint20.
Using var is not possible for function parameters or return parameters.
State variables of integer and bytesXX types can be declared as constant.

uint constant x = 32;
bytes3 constant text = "abc"

Integer Literals

As long as integer literals combined with themselves, thiervis no determination of literal integers.
This example futher explains it:

var x = 1 - 2

The value of 1 - 2 is -1, which is assigned to x and thus x receives the type int8 -- the smallest type that contains -1.
The following code snippet behaves differently, though:

var one = 1
var two = 2
var x = one - two

Here, one and two both have type uint8 which is also propagated to x.
The subtraction inside the type uint8 causes wrapping and thus the value of x will be 255.
It is even possible to temporarily exceed the maximum of 256 bits as long as only integer literals are used for the computation:

var x = (0xffffffffffffffffffff * 0xffffffffffffffffffff) * 0

Here, x will have the value 0 and thus the type uint8.

Next Lesson

Summary of Lessons

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?